Jump to content


Photo

output correct but repetitive code.


  • Please log in to reply
3 replies to this topic

#1 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 March 2006 - 05:59 PM

That code output the the row titles below each other so unfortunately did not work. I have found a way to make it output how i want to using this code:

$query = "select * from orders";
$result = mysql_query($query,$connect)
or die("sql_del: ".mysql_error($connect));

$query1 = "select * from orders";
$result1 = mysql_query($query1,$connect)
or die("sql_del: ".mysql_error($connect));


echo "<table border='2' cellpadding='5'>\n";
echo "<tr><td>orderID</td>";
while($row = mysql_fetch_array($result))
{
echo "<td>";
echo ''.$row['orderID'].'';
echo "</td>";
}
echo "</tr>";
echo "<tr><td>Title</td>";
while($row = mysql_fetch_array($result1))
{
echo "<td>";
echo ''.$row['title'].'';
echo "</td>";
}
echo"</tr>";

echo '</table>';

The only problem i am having with this way is that as you can see the code highlighted in bold is repetitive but if i dont use the second part of code, the code highlighted in italic does nothing. (If i use result instead of result1). Why is it that i can only use result in the array once?? As i have a nuber of columns i am going to have to do this a number of times but i shouldnt need to should i??


#2 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 25 March 2006 - 06:12 PM

if you've already gone through all the results with a while loop, then you can just use mysql_data_seek to go back to the start.


mysql_data_seek($result, 0);

[a href=\"http://www.php.net/mysql_data_seek\" target=\"_blank\"]http://www.php.net/mysql_data_seek[/a]
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!

#3 OriginalSunny

OriginalSunny
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts

Posted 25 March 2006 - 06:53 PM

I am trying to use that but it doesnt seem to work. The page doesn't load and just crashes. Am i using it wrong?

while($row = mysql_data_seek($result,0))
{
echo "<td>";
echo ''.$row['title'].'';
echo "</td>";
}
echo"</tr>";

#4 redbullmarky

redbullmarky
  • Staff Alumni
  • Advanced Member
  • 2,863 posts
  • LocationBedfordshire, England

Posted 26 March 2006 - 01:21 PM

[!--quoteo(post=358330:date=Mar 25 2006, 07:53 PM:name=OriginalSunny)--][div class=\'quotetop\']QUOTE(OriginalSunny @ Mar 25 2006, 07:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I am trying to use that but it doesnt seem to work. The page doesn't load and just crashes. Am i using it wrong?

while($row = mysql_data_seek($result,0))
{
echo "<td>";
echo ''.$row['title'].'';
echo "</td>";
}
echo"</tr>";
[/quote]

yes you're using it wrong here. what data seek does is returns the internal pointer to 0 or wherever you specify. so for example:

$query = "select * from thistable";
$result = mysql_query($query) or die('Whoops - '.mysql_error());

while ($row = mysql_fetch_assoc($result)
{
   echo $row['Name'];
}

mysql_data_seek($result, 0);

while ($row = mysql_fetch_assoc($result)
{
   echo $row['Name'];
}

the above will print the same set of information twice without using two seperate queries/results.

on a sidenote, the reason why your browser crashed with the code you used is because mysql_data_seek returns true if it's successful. As it had no problem working, it was always true so your while loop carried on going...and going...

Hope that helps
Cheers
Mark
"you have to keep pissing in the wind to learn how to keep your shoes dry..."

I say old chap, that is rather amusing!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users