Jump to content


Photo

looping help -> works on one but not the other


  • Please log in to reply
2 replies to this topic

#1 jeffshen

jeffshen
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 06 August 2006 - 07:02 PM

Hey,

I have a previous snippet of code which I have been using for ages now,

<?PHP
include("login.php");
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM people";
$result=mysql_query($query);
$num=mysql_numrows($result); 

mysql_close();

echo "<b><center>2005-2006</center></b><br><br>";

?>
<table border="1" cellspacing="2" cellpadding="2">
<tr> 
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Nickname</font></th>
<th><font face="Arial, Helvetica, sans-serif">Class</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-Mail</font></th>
</tr>

<?PHP
$i=0;
while ($i < $num) {
$surname=mysql_result($result,$i,"surname");
$name=mysql_result($result,$i,"name");
$nickname=mysql_result($result,$i,"nickname");
$class=mysql_result($result,$i,"class");
$email=mysql_result($result,$i,"e_mail");
?>

<tr> 
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name"; echo" $surname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$nickname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$class"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>"><? echo "$email"; ?></a></font></td>
</tr>
<?
++$i;
} 
echo "</table>";?>

but however when I port the whole thing to another environment it dosent work... the MySQL query only returns the first line in this case rather than all the database entries as specified in the code...

<table border="1" cellspacing="2" cellpadding="2">
<tr> 
<th><font face="Arial, Helvetica, sans-serif">Symbol</font></th>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
<th><font face="Arial, Helvetica, sans-serif">Last</font></th>
<th><font face="Arial, Helvetica, sans-serif">Change</font></th>
<th><font face="Arial, Helvetica, sans-serif">High</font></th>
<th><font face="Arial, Helvetica, sans-serif">Low</font></th>
</tr>

<?php
	 include("login.php");
	 mysql_connect(localhost,$username,$password);
	 @mysql_select_db($database) or die( "Unable to select database");
	 $query="SELECT * FROM stocks";
	 $result=mysql_query($query);
	 $num=mysql_numrows($result); 
	 mysql_close();
	 $i=0;
	 while ($i < $num) {
	 $quote = mysql_result($result,$i,"symbol");
	 $standard = ".hk";
	 $findquote = "$quote$standard";

	 Class yahoo
	 {
	 function get_stock_quote($symbol)
	 {
	 	$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=snl1d1t1c1ohgv" ,$symbol);
		$fp = fopen($url, "r");
		if(!fp)
		{
			echo "error : cannot recieve stock quote information";
		}
		else
		{
			$array = fgetcsv($fp , 4096 , ', ');
			fclose($fp);
			$this->symbol = $array[0];
			$this->name = $array[1];
			$this->last = $array[2];
			$this->date = $array[3];
			$this->time = $array[4];
			$this->change = $array[5];
			$this->open = $array[6];
			$this->high = $array[7];
			$this->low = $array[8];
			$this->volume = $array[9];
		}
	 }
	 }
	 $quote = new yahoo;
	 $quote->get_stock_quote("$findquote");
?>

<tr> 
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->symbol"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->name"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->time"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->date"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->last"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->change"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->high"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->low"; ?></font></td>
</tr>
<?
	 ++$i;
	 } 
	 ?>
</table>

Can someone please help?

Thanks a lot,

Jeff

#2 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 06 August 2006 - 07:50 PM

this now should work,

fixed localhost to "localhost"
removed the mysql_numrows() function, misspelled must be mysql_num_rows()
changed the loop from while($i < $num) to while ($row = mysql_fetch_array($result))
removed mysql_result();
changed all variables inside the while loop
fixed some unneccessary typing echo $name; echo $surname;
mysql_close() now executes before php is closed instead of behind the mysql_num_rows() function

<?PHP
include("login.php");
mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM people";
$result=mysql_query($query);

echo "<b><center>2005-2006</center></b><br><br>";

?>
<table border="1" cellspacing="2" cellpadding="2">
<tr> 
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Nickname</font></th>
<th><font face="Arial, Helvetica, sans-serif">Class</font></th>
<th><font face="Arial, Helvetica, sans-serif">E-Mail</font></th>
</tr>

<?PHP
$i=0;
while ($row = mysql_fetch_array($result)) {
   $surname=$row['surname'];
   $name=$row['name'];
   $nickname=$row['nickname'];
   $class=$row['class'];
   $email=$row['e_mail'];
?>

<tr> 
<td><font face="Arial, Helvetica, sans-serif"><? echo "$name" . " $surname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$nickname"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$class"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><a href="mailto:<? echo "$email"; ?>"><? echo "$email"; ?></a></font></td>
</tr>
<?
++$i;
} 
echo "</table>";

mysql_close();
?>


#3 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,431 posts
  • LocationBelgium

Posted 06 August 2006 - 08:00 PM

<table border="1" cellspacing="2" cellpadding="2">
<tr> 
<th><font face="Arial, Helvetica, sans-serif">Symbol</font></th>
<th><font face="Arial, Helvetica, sans-serif">Name</font></th>
<th><font face="Arial, Helvetica, sans-serif">Time</font></th>
<th><font face="Arial, Helvetica, sans-serif">Date</font></th>
<th><font face="Arial, Helvetica, sans-serif">Last</font></th>
<th><font face="Arial, Helvetica, sans-serif">Change</font></th>
<th><font face="Arial, Helvetica, sans-serif">High</font></th>
<th><font face="Arial, Helvetica, sans-serif">Low</font></th>
</tr>

<?php
	 include("login.php");
	 mysql_connect("localhost",$username,$password);
	 @mysql_select_db($database) or die( "Unable to select database");
	 $query="SELECT * FROM stocks";
	 $result=mysql_query($query);
	 $i=0;
	 while ($row = mysql_fetch_array($result)) {
	 $quote = $row['symbol'];
	 $standard = ".hk";
	 $findquote = "$quote$standard";

	 Class yahoo
	 {
	 function get_stock_quote($symbol)
	 {
	 	$url = sprintf("http://finance.yahoo.com/d/quotes.csv?s=%s&f=snl1d1t1c1ohgv" ,$symbol);
		$fp = fopen($url, "r");
		if(!$fp)
		{
			echo "error : cannot recieve stock quote information";
		}
		else
		{
			$array = fgetcsv($fp , 4096 , ', ');
			fclose($fp);
			$this->symbol = $array[0];
			$this->name = $array[1];
			$this->last = $array[2];
			$this->date = $array[3];
			$this->time = $array[4];
			$this->change = $array[5];
			$this->open = $array[6];
			$this->high = $array[7];
			$this->low = $array[8];
			$this->volume = $array[9];
		}
	 }
	 }
	 $quote = new yahoo;
	 $quote->get_stock_quote("$findquote");
?>

<tr> 
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->symbol"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->name"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->time"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->date"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->last"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->change"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->high"; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo "$quote->low"; ?></font></td>
</tr>
<?
	++$i;
	} 
	?>
</table>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users