Forum
php mysql nesting while loops
Neil-C
Contact in PM
Neil-C
Messages count : 11
Likes count : 0
Registration :
20 December 2007
I'm trying to display a comment thread per design for my graphic design portfolio.
Below is the code i have at the minute, I hope someone out there can point me in the right direction. I'm relatively new to php so be nice with me. :confused2
I have 2 tables in my database, one containing all the image paths and description, another table for the comments.
[PHP]<?php
$mysqli = mysqli_connect("localhost", "root", "password", "database");
$get_info_sql = "SELECT id, thumb, full_size, description FROM graphic_design";
$get_info_res = mysqli_query($mysqli, $get_info_sql) or die (mysqli_error($mysqli));
$get_comments_sql = "SELECT design_id, user_name, user_comment FROM comments WHERE design_id = '".$id."'";
$get_comments_res = mysqli_query($mysqli, $get_comments_sql) or die (mysqli_error($mysqli));
if (mysql_get_num_rows($get_info_res) > 0) {
while ($image_info = mysqli_fetch_array($get_info_res) {
$id = $image_info['id'];
$thumb = $image_info['thumb'];
$full_size = $image_info['full_size'];
$description = $image_info['description'];
$display_block .= "<div><a href=\"".$full_size."\"><img src=\"".$thumb."\"/></a><p>".$description."</p>";
}
while ($comments = mysqli_fetch_array($get_comments_res) {
$design_id = $comments['design_id'];
$user_name = $comments['user_name'];
$user_comment = $comments['user_comment'];
$display_block .= "<p><b>".$user_name.":</b><br/>".$user_comment."</p></div>";
}
mysqli_free_result($get_info_res);
}
?>[/PHP]
Below is the code i have at the minute, I hope someone out there can point me in the right direction. I'm relatively new to php so be nice with me. :confused2
I have 2 tables in my database, one containing all the image paths and description, another table for the comments.
[PHP]<?php
$mysqli = mysqli_connect("localhost", "root", "password", "database");
$get_info_sql = "SELECT id, thumb, full_size, description FROM graphic_design";
$get_info_res = mysqli_query($mysqli, $get_info_sql) or die (mysqli_error($mysqli));
$get_comments_sql = "SELECT design_id, user_name, user_comment FROM comments WHERE design_id = '".$id."'";
$get_comments_res = mysqli_query($mysqli, $get_comments_sql) or die (mysqli_error($mysqli));
if (mysql_get_num_rows($get_info_res) > 0) {
while ($image_info = mysqli_fetch_array($get_info_res) {
$id = $image_info['id'];
$thumb = $image_info['thumb'];
$full_size = $image_info['full_size'];
$description = $image_info['description'];
$display_block .= "<div><a href=\"".$full_size."\"><img src=\"".$thumb."\"/></a><p>".$description."</p>";
}
while ($comments = mysqli_fetch_array($get_comments_res) {
$design_id = $comments['design_id'];
$user_name = $comments['user_name'];
$user_comment = $comments['user_comment'];
$display_block .= "<p><b>".$user_name.":</b><br/>".$user_comment."</p></div>";
}
mysqli_free_result($get_info_res);
}
?>[/PHP]
-
Victorious
Messages count : 20Likes count : 0Registration : 3 May 2010
Why not use foreach loop?Neil-C, post: 14569 a écrit : I'm trying to display a comment thread per design for my graphic design portfolio.
Below is the code i have at the minute, I hope someone out there can point me in the right direction. I'm relatively new to php so be nice with me. :confused2
I have 2 tables in my database, one containing all the image paths and description, another table for the comments.
[PHP]<?php
$mysqli = mysqli_connect("localhost", "root", "password", "database");
$get_info_sql = "SELECT id, thumb, full_size, description FROM graphic_design";
$get_info_res = mysqli_query($mysqli, $get_info_sql) or die (mysqli_error($mysqli));
$get_comments_sql = "SELECT design_id, user_name, user_comment FROM comments WHERE design_id = '".$id."'";
$get_comments_res = mysqli_query($mysqli, $get_comments_sql) or die (mysqli_error($mysqli));
if (mysql_get_num_rows($get_info_res) > 0) {
while ($image_info = mysqli_fetch_array($get_info_res) {
$id = $image_info['id'];
$thumb = $image_info['thumb'];
$full_size = $image_info['full_size'];
$description = $image_info['description'];
$display_block .= "<div><a href=\"".$full_size."\"><img src=\"".$thumb."\"/></a><p>".$description."</p>";
}
while ($comments = mysqli_fetch_array($get_comments_res) {
$design_id = $comments['design_id'];
$user_name = $comments['user_name'];
$user_comment = $comments['user_comment'];
$display_block .= "<p><b>".$user_name.":</b><br/>".$user_comment."</p></div>";
}
mysqli_free_result($get_info_res);
}
?>[/PHP]