?
Solved

Store values from API call into database

Posted on 2011-03-24
4
Medium Priority
?
424 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
Suggested Courses

588 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