Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Store values from API call into database

I am doing some api calls to pull in data from Clicky and display it for our end users based on a date range.  I want to store the information in a database now.  I am green to php and am needing some help getting starting with this.  Essentially when a date is entered and submitted into the text box I want to grab all the values and put them into a database.  See form code below.
<form name="test" type="get">
Enter Month:
 <input name="date" type="text" size="15" value="<?=$date?>">
  <br /> Format yyyy-mm,then hit [enter]
</form>
<p>&nbsp;</p>
<p><strong>Date:</strong>
<?=$_GET['date'];?></p>
<?

$date = $_GET['date'];
include "config.php";


?>


<?php


echo "<pre>";

//Getting page views for showrooms

$v = file_get_contents("http://api.getclicky.com/api/stats/4?site_id=$site_id&sitekey=$sitekey&type=pages&limit=all&filter=%2F$companyName&date=$date&output=php");


$views = unserialize($v);

//initialize variable to 0
$total['homepage'] = 0;
$total['contact'] = 0;
$total['general'] = 0;
$total['guides'] = 0;
$total['products'] = 0;
$total['related'] = 0;
$grandtotal = 0;

//Loop through the page views
foreach($views['pages'][$key] as $page){
	$grandtotal += $page ['value']; 
	switch ($page['url']) {
		case 'http://thefabricator.com/showroom/$companyName' :
			$total ['homepage'] = $page ['value'];
			break;		
		case 'http://thefabricator.com/showroom/$companyName/contact' :
		 	$total['contact'] = $page ['value'];	
			break;
		case 'http://thefabricator.com/showroom/$companyName/general' :
		//add slashes to all urls
		case 'http://thefabricator.com/showroom/$companyName/general/' :
		 	$total['general'] = $page ['value'];	
			break;
		case 'http://thefabricator.com/showroom/$companyName/guides' :
		 	$total['guides'] = $page ['value'];	
			break;
		case 'http://thefabricator.com/showroom/$companyName/products' :
		 	$total['products'] = $page ['value'];	
			break;
		case 'http://thefabricator.com/showroom/$companyName/related' :
		 	$total['related'] = $page ['value'];	
			break;
			
		// Things to ignore
		case "http://thefabricator.com/showroom/$companyName/manage";
			// Any link between this and the other comment will be ignored
		break;
		default:
			$linksclicked += $page['value'];
			$links[] = array('url' => $page['url'], 'value' => $page ['value']);
			break;
						
		}
	
}




//Getting showroom visitors per sections

$m = file_get_contents("http://api.getclicky.com/api/stats/4?site_id=$site_id&sitekey=$sitekey&type=visitors-list&limit=all&href=%2Fshowroom%2F$companyName&date=$date&output=php");

$m = unserialize($m);

$main = count($m['visitors-list'][$key]);

$c = file_get_contents("http://api.getclicky.com/api/stats/4?site_id=$site_id&sitekey=$sitekey&type=visitors-list&limit=all&href=%2Fshowroom%2F$companyName%2Fcontact&date=$date&output=php");

$c = unserialize($c);

$contact = count($c['visitors-list'][$key]);

$gen = file_get_contents("http://api.getclicky.com/api/stats/4?site_id=$site_id&sitekey=$sitekey&type=visitors-list&limit=all&href=%2Fshowroom%2F$companyName%2Fgeneral&date=$date&output=php");

$gen = unserialize($gen);

$general = count($gen['visitors-list'][$key]);


$g = file_get_contents("http://api.getclicky.com/api/stats/4?site_id=$site_id&sitekey=$sitekey&type=visitors-list&limit=all&href=%2Fshowroom%2F$companyName%2Fguides&date=$date&output=php");

$g = unserialize($g);

$guides = count($g['visitors-list'][$key]);

$p = file_get_contents("http://api.getclicky.com/api/stats/4?site_id=$site_id&sitekey=$sitekey&type=visitors-list&limit=all&href=%2Fshowroom%2F$companyName%2Fproducts&date=$date&output=php");


$p = unserialize($p);

$products = count($p['visitors-list'][$key]);

$r = file_get_contents("http://api.getclicky.com/api/stats/4?site_id=$site_id&sitekey=$sitekey&type=visitors-list&limit=all&href=%2Fshowroom%2F$companyName%2Frelated&date=$date&output=php");

$r = unserialize($r);

$related = count($r['visitors-list'][$key]);
?>	


	
<table width="350" border="1" class="resources">
<thead>
 <tr>
  <th><strong>Page</strong></th>
  <th>Views</th>
  <th><strong>No. of Visitors</strong></th>
 </tr>
 </thead>
 <tr>
   <td>Main Page </td>
   <td><?=$total['homepage']?></td>
  <td><?=$main?>  </td>
 </tr>
  <tr>
   <td class="odd">Contact </td>
   <td class="odd"><?=$total['contact']?></td>
 <td class="odd"><?=$contact?>  </td>
 </tr>
 <tr>
  <td>General</td>
  <td><?=$total['general']?></td>
  <td><?=$general?> </td>
 </tr>
 <tr>
  <td class="odd">Guides</td>
  <td class="odd"><?=$total['guides']?></td>
 <td class="odd"><?=$guides?> </td>
 </tr>
 <tr>
  <td>Products</td>
  <td><?=$total['products']?></td>
  <td><?=$products?></td>
 </tr>
 <tr>
 <td class="odd">Related</td>
 <td class="odd"><?=$total['related']?></td>
 <td class="odd"><?=$related?></td>
 </tr>
 <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
 </tr>
 <tr>
  <td><strong>Totals</strong></td>
  <td><?=$grandtotal?></td>
  <td><?=$related + $contact + $products + $guides + $general + $main?></td>
 </tr>
</table>
<p>&nbsp;</p>

 
<p>&nbsp;</p>




 </table>

Open in new window

0
Albee_J
Asked:
Albee_J
  • 2
  • 2
1 Solution
 
Albee_JAuthor Commented:
I know how to do an insert with a standard form, just not sure how to do an insert with existing variables.... save them, and put them in db.
0
 
Ray PaseurCommented:
To do this, you need to get some foundation in how PHP and Data bases work together.  I recommend this book - very readable with great examples.
http://www.sitepoint.com/books/phpmysql4/

The example in the code snippet show you how to access the URL parameters or the form input when the form method is GET.  Note that it depends on the use of the PHP short-open-tag.  Normally you would want to avoid those because of potential conflicts with XML.  Install this and run it, then post back with any specific questions.

Best regards, ~Ray
<?php // RAY_get_example.php
error_reporting(E_ALL);

// FROM THE OP
// ?email=stevecase@aol.com&color=blue

// DEFAULT VALUES ARE EMPTY
$email = '';
$color = '';
$pword = '';

// IF THERE IS DATA
if (!empty($_GET))
{
    if (isset($_GET["email"])) $email = $_GET["email"];
    if (isset($_GET["color"])) $color = $_GET["color"];
    if (isset($_GET["pword"])) $pword = $_GET["pword"];

}
// END OF PHP PROCESSING
?>
Enter data in these fields and watch the browser address bar change on submit
<form action="<?=$_SERVER["PHP_SELF"]?>">
Email: <input name="email" value="<?=$email?>" />
Color: <input name="color" value="<?=$color?>" />
PWord: <input name="pword" value="<?=$pword?>" type="password" />
<input type="submit" />
</form>
<br/><br/>
<?php
echo "<pre>\n";

echo "GET: ";
var_dump($_GET);

echo "REQUEST_URI ";
var_dump($_SERVER["REQUEST_URI"]);

echo "PHP_SELF ";
var_dump($_SERVER["PHP_SELF"]);

Open in new window

0
 
Albee_JAuthor Commented:
Thanks!
0
 
Ray PaseurCommented:
Thanks for the points - it's a great question.  And buy that book -- you will love it and you will be very happy with the quick results you can get from its examples.  ~Ray
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now