Reputation: 11471
I am new to PHP and I am having issues pulling this data in.. It is finding the records according to the debugger but then for some reason the page comes blank. Can anyone please help me with this for loop to retrieve the fields?, i think this is where the issue is. I tried with and without that while loop (which i think is wrong there) and nothing
echo '<table class="table_content">
<tr>
<td class="td_order" colspan="3" width="800px">
Descriptions </td>
</tr><tr>';
for($counter = 0; $row2 = mysql_fetch_row($result2); $counter++)
{
while ($row2 = mysql_fetch_assoc($result)) {
$_name = $row["_name"];
$_image = $row["_image"];
$_disc = $row["_disc"];
}
print("<td valign='top' width='230px' height='300px'>");
print("<p style='font-size:18px; color:blue; padding:0;'>$_name</p>");
print("<img class='custom' alt='' src='$_image'/>");
print("<p>$_disc</p>");
print('</td>');
}
}
echo '</tr></table> ';
I think I am doing something wrong with this logic for the loop, I have tried several things.. but nothing... I would appreciate your help.
PS: The connections works, the query works.. The data being retrieve is as foollow
_name _image _disc
Benjamin images/benjamin.jpg He's a great person
Jairo images/jairo.jpg Jairo has experience as a civil engineer..
Upvotes: 0
Views: 166
Reputation: 14149
You're fetching the row twice which means you'll not echo half the rows. This solution keeps things a bit cleaner with foreach and some inline echoing of the variables. If PHP short tags are enabled you can use <?= $foo ?>
in place of <?php echo $foo ?>
<?php
while($row = mysql_fetch_assoc($result2)) {
?>
<td valign="top" width="230px" height="300px">
<p style="font-size:18px; color:blue; padding:0;"><?php echo $row['_name'] ?></p>
<img class="custom" alt="" src='<?php echo $row['_image'] ?>"/>
<p><?php echo $row['_disc'] ?></p>
</td>
<?php
}
?>
Upvotes: 0
Reputation: 3035
I'd suggest adding a bit more professional approach to your application and deploying a template system like Smarty. You'll benefit from separating your view from the logic, have caching and easily maintainable code.
In the circumstances, you could solve your problem in a following way: Example 7.33. Database example with {foreachelse}:
<?php
include('Smarty.class.php');
$smarty = new Smarty;
$dsn = 'mysql:host=localhost;dbname=test';
$login = 'test';
$passwd = 'test';
// setting PDO to use buffered queries in mysql is
// important if you plan on using multiple result cursors
// in the template.
$db = new PDO($dsn, $login, $passwd, array(
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
$res = $db->prepare("select * from users");
$res->execute();
$res->setFetchMode(PDO::FETCH_LAZY);
// assign to smarty
$smarty->assign('res',$res);
$smarty->display('index.tpl');?>
?>
Upvotes: 1
Reputation: 4347
Remove the for and while loop and change it with ;
while($row = mysql_fetch_array( $result )){
$_name = $row["_name"];
$_image = $row["_image"];
$_disc = $row["_disc"];
..
Upvotes: 1