iKettles 08-04-2008, 11:01 PM I've got some code that echoes some data from a table but unless the echo code is within the code that connects to the database (eg. I can't simply echo anywhere on the page, it HAS to be in the same PHP tags as the code that connects/pulls the data) it only shows one line of data. Any reason for this?
ramm19 08-04-2008, 11:10 PM Please post your code so we can tell you where the problem is.
You can echo your DB results anywhere in that PHP file (e.g. inside your html code).
iKettles 08-04-2008, 11:13 PM Please post your code so we can tell you where the problem is.
You can echo your DB results anywhere in that PHP file (e.g. inside your html code).
Yeah, that's what I'm trying to do but I can only seem to be able to echo within the code that pulls the data from the database.
<?php
$username="username";
$password="password";
$database="dbname";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
$result=mysql_query($query);
$myrow = mysql_fetch_assoc($result);
do {
$Fullname=$myrow['Fullname'];
$Callsign=$myrow['Callsign'];
$Flag=$myrow['Flag'];
echo "<table width=200><td>$Callsign - $Fullname</td><td><img src=$Flag></td><tr></tr></table>";
} while ($myrow = mysql_fetch_assoc($result));
?>
ramm19 08-04-2008, 11:19 PM is that code working? post the code containing the html that doesn't work.
btw, this is how would do it:
<?php
$username="username";
$password="password";
$database="dbname";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo "<table width=200><td>$myrow['Callsign'] - $myrow['Fullname']</td><td><img src=$myrow['Flag']></td><tr></tr></table>";
}
mysql_free_result($result);
?>
iKettles 08-04-2008, 11:21 PM is that code working? post the code containing the html that doesn't work.
btw, this is how would do it:
<?php
$username="username";
$password="password";
$database="dbname";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
$result=mysql_query($query);
while ($row = mysql_fetch_assoc($result)) {
echo "<table width=200><td>$myrow['Callsign'] - $myrow['Fullname']</td><td><img src=$myrow['Flag']></td><tr></tr></table>";
}
mysql_free_result($result);
?>
Yeah that's the code with the HTML that works. If I just take out the echo bit and move it to some where else in the page (outside of that code) it will only display one result.
derzok 08-04-2008, 11:23 PM <?php
$username="username";
$password="password";
$database="dbname";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM newpilots ORDER BY Fullname DESC LIMIT 5";
$result=mysql_query($query);
echo "<table width=200>"
while ($myrow = mysql_fetch_assoc($result)) {
echo "<tr>"
$Fullname=$myrow['Fullname'];
$Callsign=$myrow['Callsign'];
$Flag=$myrow['Flag'];
echo "<td>$Callsign - $Fullname</td><td><img src=$Flag></td></tr>"
}
echo "</table>";
?>
Sorry, I hate do/while syntax :p
Honestly, I don't see anything wrong with the code you posted aside from the HTML (which I corrected). It should output at MOST 5 rows (according to your query). Unless there's some minor error in your do/while that I missed... What happens when you run my version?
ramm19 08-04-2008, 11:24 PM Yeah that's the code with the HTML that works. If I just take out the echo bit and move it to some where else in the page (outside of that code) it will only display one result.
that is because you are not using the while loop in your html code...
post the part of the code that doesn't work and I'll tell you have to do it, it's very simple.
masterofollies 08-04-2008, 11:35 PM If you have multiple items displayed, like Full Name, if you want all the full names in the database to show on the page in line, one after the other, then you must put your table inside the while loop.
iKettles 08-04-2008, 11:40 PM Sorry, I hate do/while syntax :p
Honestly, I don't see anything wrong with the code you posted aside from the HTML (which I corrected). It should output at MOST 5 rows (according to your query). Unless there's some minor error in your do/while that I missed... What happens when you run my version?
Still only displays one result.
that is because you are not using the while loop in your html code...
post the part of the code that doesn't work and I'll tell you have to do it, it's very simple.
I have posted the part of the code that doesn't work.
ramm19 08-04-2008, 11:46 PM ic, you HTML is all messed up... use this:
echo "<table width=200>";
while ($row = mysql_fetch_assoc($result)) {
echo"
<tr>
<td>$myrow['Callsign'] - $myrow['Fullname']</td>
<td><img src=\"$myrow['Flag']\"></td>
</tr>
";
}
echo "</table>";
how many rows do you have?
iKettles 08-04-2008, 11:54 PM ic, you HTML is all messed up... use this:
echo "<table width=200>";
while ($row = mysql_fetch_assoc($result)) {
echo"
<tr>
<td>$myrow['Callsign'] - $myrow['Fullname']</td>
<td><img src=\"$myrow['Flag']\"></td>
</tr>
";
}
echo "</table>";
how many rows do you have?
One row. As for the table tags, they're outside of the PHP code itself.
ramm19 08-05-2008, 12:22 AM put the code I've showed you anywhere you want... of course they have to be inside PHP tags.
If you have 1 row in your DB it will only show 1 result...
iKettles 08-05-2008, 12:36 AM put the code I've showed you anywhere you want... of course they have to be inside PHP tags.
If you have 1 row in your DB it will only show 1 result...
Sorry I read your question wrong, I have 5 rows.
The problem is nothing to do with the HTML code as just simply echoing the variable with no formatting whatsoever still only shows one result when the echo isn't inside the main PHP tag with the database stuff itself.
dumpfi 08-05-2008, 09:12 AM You must output the variables within the while, because with each iteration the variables' contents get updated to the values of the next row. When the while is left the variables hold the values of the last row returned.
Only way to overcome this situation is to store each row in an array, e.g:
$myArray = array();
while ($myrow = mysql_fetch_assoc($result)) {
$myArray[] = $myrow; // fill $myArray with the contents of the query result
}
// do something
// output the contents of myArray
foreach($myArray as $myrow) {
echo "<tr>" ;
$Fullname=$myrow['Fullname'];
$Callsign=$myrow['Callsign'];
$Flag=$myrow['Flag'];
echo "<td>$Callsign - $Fullname</td><td><img src=$Flag></td></tr>";
}dumpfi
iKettles 08-05-2008, 12:14 PM You must output the variables within the while, because with each iteration the variables' contents get updated to the values of the next row. When the while is left the variables hold the values of the last row returned.
Only way to overcome this situation is to store each row in an array, e.g:
$myArray = array();
while ($myrow = mysql_fetch_assoc($result)) {
$myArray[] = $myrow; // fill $myArray with the contents of the query result
}
// do something
// output the contents of myArray
foreach($myArray as $myrow) {
echo "<tr>" ;
$Fullname=$myrow['Fullname'];
$Callsign=$myrow['Callsign'];
$Flag=$myrow['Flag'];
echo "<td>$Callsign - $Fullname</td><td><img src=$Flag></td></tr>";
}dumpfi
Couldn't seem to be able to get those arrays to work. Is there any disadvantages of them? I did try just including the database.php file and then within the same PHP tags echo it but it didn't want to work. I'm probably just going to write out the code every time because I'm not hugely bothered about a long term fix, I thought it would be much easier than this. Thanks for the help.
derzok 08-05-2008, 01:52 PM You need to tell us what you mean when you say "things didn't work" - did it produce an error? Did it show you the wrong results? Did it show you a blank page? Is it still just showing you one row? What does happen when you run dumpfi's code?
iKettles 08-05-2008, 02:01 PM You need to tell us what you mean when you say "things didn't work" - did it produce an error? Did it show you the wrong results? Did it show you a blank page? Is it still just showing you one row? What does happen when you run dumpfi's code?
When I'd echo the array it would just print "Array". I'm unsure whether I'm using the arrays correctly however.
djm0219 08-05-2008, 02:18 PM Arrays are printed (echoed) using print_r($arrayname) not simply print or echo.
derzok 08-05-2008, 02:25 PM What djm0219 said - you've gotta use print_r or var_dump on arrays. Either that or iterate through them with a for or while loop -
foreach($array as $key=>$value) {
echo '['.$key.'] => '. $value.', ';
}
iKettles 08-05-2008, 03:00 PM When I use print_r, it prints this on the page -
Array ( [0] => Array ( [Fullname] => Shamir Fersobe [Callsign] => CAY850 [Flag] => http://www.vafinancials.com/images/Flags/DR.png ) [1] => Array ( [Fullname] => Raymond Drenthe [Callsign] => CAY191 [Flag] => http://www.vafinancials.com/images/Flags/NT.png ) [2] => Array ( [Fullname] => Quinton Crafer [Callsign] => CAY319 [Flag] => http://www.vafinancials.com/images/Flags/CA.png ) [3] => Array ( [Fullname] => Erick Santiago [Callsign] => CAY005 [Flag] => http://www.vafinancials.com/images/Flags/RQ.png ) [4] => Array ( [Fullname] => Bob Marley [Callsign] => CAY000 [Flag] => http://www.vafinancials.com/images/Flags/AA.png ) )
I'm using the exact code you gave me, no changes except removing the echoes.
|