tet3828 Posted November 7, 2006 Share Posted November 7, 2006 Here is a tricky one. At least I think. My script currently returns database search results. The information is displayed as a table with a thumbnail, item Name ect. Because this information is in a table; the results are stacked upon eachother vertically.This is going to cause a problem for the user. I tried to come up with a loop dohickey that will display each table inside of a dynamic table so the results can be displayed within a cell. I gave it a shot and tried many variations of it but no luck.where has my script gone wrong?[code] noresults = mysql_num_rows($result); $columns = 3; $rows = $noresults / $columns; $rows = ceil($rows); $i = 0; print ('<table width="100%" border="1" id="content_list">'); while($row %3[/code] Quote Link to comment Share on other sites More sharing options...
onlyican Posted November 7, 2006 Share Posted November 7, 2006 if you want a table then Read the information Below.Lets say you have a small MySQL Table, called usersWith fields set as followsid int(50) primary, auto_incrementname varchar(150)email varchar(150)pwd char(32)Now you want to show the results for all this, except the pwd (password)I would do the PHP code as follows[code]<?phpDBConnect(); //I have a function to connect to the DB, You should connect before running queries$query1 = "SELECT * FROM users";$result1 = mysql_query($query1);if($result1){if(mysql_num_rows($result1) != 0){echo "<table>\n"."<tr>\n"."<th>ID Number</th>\n"."<th>Name</th>\n"."<th>Email</th>\n"."</tr>\n";while($row1 = mysql_fetch_assoc($result1)){echo "<tr>\n"."<td>".$row1["id"]."</td>\n"."<td>".$row1["name"]."</td>\n"."<td>".$row1["email"]."</td>\n"."</tr>\n";}echo "</table>\n";}else{echo "I am afraid no results have been found<br />\n";}}else{echo "There has been an error, please try again later<br />\n";}?>[/code]With this information, you can work in your query and resultsAny problems, let us know Quote Link to comment Share on other sites More sharing options...
tet3828 Posted November 7, 2006 Author Share Posted November 7, 2006 Thanks once again for the response. it's a way better approach to my problem.I soppose I will be able to intergrate into my script but I'll need a bit more helpcomprehending this script. I understand the: [code]<tr> </tr><td> </td> [/code]concept. of course it signififyies the opening and closing of a row and columnsbut what is the function of:[code]<th> </th>and the \n ???[/code]note I am researching my question on google.but nothing beats a personal break down from a php whiz.thanks. hope someone can help. Quote Link to comment Share on other sites More sharing options...
tet3828 Posted November 7, 2006 Author Share Posted November 7, 2006 I gave the suggested script my best shot and Im getting mysql errors:The script is no longer displaying results.any suggestions?[code] if ($cat != "nocat") { $qry = "SELECT itemName,itemId,itemPrice,itemSmall FROM `products` WHERE itemCat='".$_POST['cat']."'"; $result = mysql_query($qry) or die(mysql_error()); $amount = mysql_num_rows($result); echo "<br /><br />";} while($row = mysql_fetch_array($result)) { echo "<table>\n"."<tr>\n"."<th>Item Picture</th>\n"."<th>Name</th>\n"."<th>Price</th>\n"."<th>Edit Link</th>\n"."</tr>\n";while($row1 = mysql_fetch_assoc($result1)){echo "<tr>\n"."<td><center><img src=\"".$row['itemSmall']."\" /></center></td>\n"."<td>".$row['itemName']."</td>\n"."<td>".$row['itemPrice']."</td>\n"."<td><a href=/*********.php?id=".$row['itemId']." />Edit This Item</td>\n"."</tr>\n";}echo "</table>\n";}}[/code] Quote Link to comment Share on other sites More sharing options...
chimby Posted November 7, 2006 Share Posted November 7, 2006 The "\n" is just a new line so the resulting code is easier to read...however it is not required.Where is the query for $result1?? If there isn't one there will be errors. Quote Link to comment Share on other sites More sharing options...
tet3828 Posted November 7, 2006 Author Share Posted November 7, 2006 The last reply seems to be on the right track. however my search results aren't displaying properly. its only show one result. lets say I search for items in the holiday catagory. I know for a fact I have 2 items where itemCat = "Holiday" but only one is showing. why would this happen?the code:[code]while($row = mysql_fetch_array($result)) { echo "<table border=3>\n"."<tr>\n"."<th>Item Picture</th>\n"."<th>Name</th>\n"."<th>ID #</th>\n"."<th>Catagory</th>\n"."<th>Price</th>\n"."<th>Edit Link</th>\n"."</tr>\n";while($row1 = mysql_fetch_assoc($result)){echo "<tr>\n"."<td><center><img src=\"".$row['itemSmall']."\" height=\"75\" width=\"75\" /></center></td>\n"."<td>".$row['itemName']."</td>\n"."<td>".$row['itemId']."</td>\n"."<td>".$row['itemCat']."</td>\n"."<td>".$row['itemPrice']."</td>\n"."<td><a href=/shell/data/edit.php?id=".$row['itemId']." />Edit This Item</td>\n"."</tr>\n";}}}echo "</table>\n"; [/code] Quote Link to comment Share on other sites More sharing options...
Orio Posted November 7, 2006 Share Posted November 7, 2006 I have a question about this line-[code]while($row1 = mysql_fetch_assoc($result1)){[/code]Where is $result1 set?Orio. Quote Link to comment Share on other sites More sharing options...
tet3828 Posted November 7, 2006 Author Share Posted November 7, 2006 The $result1 was a typeo. I've changed it to $result if you look at my last script posting you'll see the change. I didn't post the script right away. here it is again:[code]while($row = mysql_fetch_array($result)) { \\\ I somewhat understand the loop concept but, I don't understand the condition here.....echo "<table border=3>\n"."<tr>\n"."<th>Item Picture</th>\n"."<th>Name</th>\n"."<th>ID #</th>\n"."<th>Catagory</th>\n"."<th>Price</th>\n"."<th>Edit Link</th>\n"."</tr>\n";while($row1 = mysql_fetch_assoc($result)){ \\\ Nor here.echo "<tr>\n"."<td><center><img src=\"".$row['itemSmall']."\" height=\"75\" width=\"75\" /></center></td>\n"."<td>".$row['itemName']."</td>\n"."<td>".$row['itemId']."</td>\n"."<td>".$row['itemCat']."</td>\n"."<td>".$row['itemPrice']."</td>\n"."<td><a href=/****/****/****.php?id=".$row['itemId']." />Edit This Item</td>\n"."</tr>\n";}}}echo "</table>\n"; [/code]Oh boy I am a forum regular now....ugh that happened quick. Quote Link to comment Share on other sites More sharing options...
Orio Posted November 7, 2006 Share Posted November 7, 2006 I dont understand why you have two whiles... Try this please:[code]<?phpecho "<table border=3>\n"."<tr>\n"."<th>Item Picture</th>\n"."<th>Name</th>\n"."<th>ID #</th>\n"."<th>Catagory</th>\n"."<th>Price</th>\n"."<th>Edit Link</th>\n"."</tr>\n";while($row = mysql_fetch_array($result)){ echo "<tr>\n" ."<td><center><img src=\"".$row['itemSmall']."\" height=\"75\" width=\"75\" /></center></td>\n" ."<td>".$row['itemName']."</td>\n" ."<td>".$row['itemId']."</td>\n" ."<td>".$row['itemCat']."</td>\n" ."<td>".$row['itemPrice']."</td>\n" ."<td><a href=/shell/data/edit.php?id=".$row['itemId']." />Edit This Item</td>\n" ."</tr>\n";}echo "</table>\n";?>[/code]Orio. Quote Link to comment Share on other sites More sharing options...
onlyican Posted November 8, 2006 Share Posted November 8, 2006 You have 2 while loops?Whyyour top one should be something likeif(mysql_num_rows($result) != 0){Which means, IF the number of results from the query is NOT equal to 0 then continue.Cos if there are no rows, then we dont want to show an empty tableThe While loopwhile($row = mysql_fetch_assoc($result1)){ORwhile($row = mysql_fetch_array($result1)){basicly this gets the results from the query, and each row is placed in the car $row as a arrayif u dont use a while loop, you will only get one row. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.