Solved

Store values from API call into database

Posted on 2011-03-24
4
407 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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thanks!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now