Solved

Store values from API call into database

Posted on 2011-03-24
4
413 Views
Last Modified: 2012-05-11
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
Comment
Question by:Albee_J
  • 2
  • 2
4 Comments
 

Author Comment

by:Albee_J
ID: 35210629
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
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 35214870
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
 

Author Closing Comment

by:Albee_J
ID: 35216012
Thanks!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 35216412
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question