Page 1 of 1

How can I optimize this code? Sending xml data to mysql

Posted: Sat Jul 10, 2010 10:08 pm
by mb1
Hi,

I hope you can help. I am querying 2 xml files (ultimately there will be more than just 2), parsing them and then sending them to my sql db.

The way I have it currently, I am doing everything twice and 99% of the code is similar. There has to be a way to combine them someone with a loop.

Could you help?

Thank you,

Marine

Code: Select all

<?php 
  // set the XML file name as a PHP string
  $file = "http://api.foursquare.com/v1/venue?vid=1064544" ; 
  // load the XML file 
  $xml = @simplexml_load_file($file) or die ("no file loaded") ; 

  // assign the listName element to a string
	$name 	= $xml->name;
	$id = $xml->id;
	$checkins 	= $xml->stats->checkins;
	$herenow = $xml->stats->herenow;
	$address = $xml->address; 
	$crossstreet = $xml->crossstreet; 
	$city = $xml->city;
	$userid = $xml->stats->mayor->user->id;
	$firstname = $xml->stats->mayor->user->firstname;
	$lastname = $xml->stats->mayor->user->lastname;
	$photo = $xml->stats->mayor->user->photo;
	$tips = $xml->tips->tip->text;
	$tipuserfirst = $xml->tips->tip->user->firstname;
	$tipuserlast = $xml->tips->tip->user->lastname;
	$tipuserid = $xml->tips->tip->user->id;
    echo '<div class="venue"><a href="http://foursquare.com/venue/'.$id.'" target="_blank"><h2 class="name">'.$name."</h2></a>";
	echo '<div class="address">'.$address.' '.$crossstreet.' '.$city.'</div>';
	echo '<div class="herenow">'.$herenow.' people here right now.</div>';
	echo '<div class="checkins">'.$checkins.' total checkins. </div>';
	echo '<div class="mayor"><a href="http://foursquare.com/user/-'.$userid.'">'.$firstname.' '.$lastname.'</a></div>';
	echo '</div>';
	
	require_once('connectvars.php');
	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die( "Unable to select database");
	
	$query ="INSERT INTO `mb_foursquare`.`venues` (`id`, `v_id`, `v_name`, `v_address`, `v_mayor`, `v_mayor_url`) VALUES (NULL, '$id', '$name', '$address', '$firstname', '$userid')";
	
	mysqli_query($dbc, $query);		
	echo 'that worked!';	
	mysqli_close($dbc);

?>



<?php 
  // set the XML file name as a PHP string
  $file = "http://api.foursquare.com/v1/venue?vid=1633541" ; 
  // load the XML file 
  $xml = @simplexml_load_file($file) or die ("no file loaded") ; 

  // assign the listName element to a string
	$name 	= $xml->name;
	$id = $xml->id;
	$checkins 	= $xml->stats->checkins;
	$herenow = $xml->stats->herenow;
	$address = $xml->address; 
	$crossstreet = $xml->crossstreet; 
	$city = $xml->city;
	$userid = $xml->stats->mayor->user->id;
	$firstname = $xml->stats->mayor->user->firstname;
	$lastname = $xml->stats->mayor->user->lastname;
	$photo = $xml->stats->mayor->user->photo;
	$tips = $xml->tips->tip->text;
	$tipuserfirst = $xml->tips->tip->user->firstname;
	$tipuserlast = $xml->tips->tip->user->lastname;
	$tipuserid = $xml->tips->tip->user->id;
    echo '<div class="venue"><a href="http://foursquare.com/venue/'.$id.'" target="_blank"><h2 class="name">'.$name."</h2></a>";
	echo '<div class="address">'.$address.' '.$crossstreet.' '.$city.'</div>';
	echo '<div class="herenow">'.$herenow.' people here right now.</div>';
	echo '<div class="checkins">'.$checkins.' total checkins. </div>';
	echo '<div class="mayor"><a href="http://foursquare.com/user/-'.$userid.'">'.$firstname.' '.$lastname.'</a></div>';
	echo '</div>';
	
	require_once('connectvars.php');
	$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die( "Unable to select database");
	
	$query ="INSERT INTO `mb_foursquare`.`venues` (`id`, `v_id`, `v_name`, `v_address`, `v_mayor`, `v_mayor_url`) VALUES (NULL, '$id', '$name', '$address', '$firstname', '$userid')";
	
	mysqli_query($dbc, $query);		
	echo 'that worked!';	
	mysqli_close($dbc);

?>


Re: How can I optimize this code? Sending xml data to mysql

Posted: Sat Jul 10, 2010 11:59 pm
by requinix
1. Don't (attempt to) include definition files like connectvars.php twice.
2. Open one database connection at the start of the file and (IMO) don't keep opening and closing it. Leave it open.
3. If you find yourself duplicating code you should probably make it as generic as possible and stick it into a function. Use arguments for stuff that varies.

Re: How can I optimize this code? Sending xml data to mysql

Posted: Mon Jul 12, 2010 7:45 am
by mb1
Thank you! It's helpful.