OreoKnight Posted February 20, 2008 Share Posted February 20, 2008 So here is the current set-up. It prints the result I'm looking one after another right under each other. What I want is the results to auto-print into a table with 3 columns and as many rows as needed (auto updates as more entries are added) Any help? All I need is the article title with link ability to show up. <div class="article_menu"><br><? // get all the articles for this category $sql = mysql_query("select * from articles where cat_id='51' order by id DESC"); $count = mysql_num_rows($sql); if($count > 0){ while($row = mysql_fetch_assoc($sql)){ $id = $row['id']; $title = $row['title']; /*if(strlen($image) > 5) print '<a href="article.php/'.$id.'/'.$title.'">'.$title.'<br /></a>'; else*/ print '<a href="../../article.php/'.$id.'/'.$title.'">'.$title.'<br /></a>'; } } else{ print "<br /><b style=\"font-size: 11px; text-decoration: underline;\">No articles currently in this category.</b><br />Currently, no articles are listed in this category. Please check back shortly for our constantly updated and listed articles!"; } ?> This is what I tried. <div class="article_menu"><br><? // get all the articles for this category $sql = mysql_query("select * from articles where cat_id='51' order by id DESC"); $count = mysql_num_rows($sql); if($count > 0){ while($row = mysql_fetch_assoc($sql)){ $id = $row['id']; $title = $row['title']; /*if(strlen($image) > 5) echo '<table><tr>';for ($i=0; $i<blah; $i++){ blah; echo '<td><a href="article.php/'.$id.'/'.$title.'">'.$title.'<br /></a>';</td>'; if ($i%3 == 0 && $i != 0) //this is if you want 3 columns echo '</tr><tr>'; //this makes a new row every 3rd column}echo '</tr></table>'; } } else{ print "<br /><b style=\"font-size: 11px; text-decoration: underline;\">No articles currently in this category.</b><br />Currently, no articles are listed in this category. Please check back shortly for our constantly updated and listed articles!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/ Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 http://www.phpfreaks.com/forums/index.php/topic,95426.0.html Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-471911 Share on other sites More sharing options...
OreoKnight Posted February 20, 2008 Author Share Posted February 20, 2008 So this is what I tried and got an error. Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/jujubai/public_html/test6.php on line 140 <?php $query = "select * from articles where cat_id='51' order by id DESC"; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { { $id = $row['id']; $title = $row['title']; // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product if($title != "" && $title != null) echo "<td><a href="article.php/'.$id.'/'.$title.'">'.$title.'<br /></a></td>"; else*/ echo "<td><a href="../../article.php/'.$id.'/'.$title.'">'.$title.'<br /></a></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-471923 Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 And line 140 is... Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-471936 Share on other sites More sharing options...
OreoKnight Posted February 20, 2008 Author Share Posted February 20, 2008 In notepad it says this is line 140. echo "<td><a href="article.php/'.$id.'/'.$title.'">'.$title.'<br /></a></td>"; What in that is wrong? It looks right to me... Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-471949 Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 You can't do double quotes in double quotes echo "<td><a href='article.php/'$id'/'$title'>$title</a></td>"; Should work Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-471960 Share on other sites More sharing options...
OreoKnight Posted February 20, 2008 Author Share Posted February 20, 2008 Okay so now there is a new error message.... Parse error: syntax error, unexpected '*' in /home/jujubai/public_html/test6.php on line 122 Line 122 is the start of the else*/ is else not compatible with the echo command? echo "<td><a href='article.php/'$id'/'$title'>$title</a></td>"; else*/ echo "<td><a href="../../article.php/'.$id.'/'.$title.'">.$title.<br /></a></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472000 Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 What is else*/ supposed to do? Is it the end of a comment area? Do you mean else { ? Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472002 Share on other sites More sharing options...
OreoKnight Posted February 20, 2008 Author Share Posted February 20, 2008 Its supposed to make sure depending on where the user comes in from that they link back to the right place. New error Parse error: syntax error, unexpected '.' in /home/jujubai/public_html/test6.php on line 124 Line 124 starts like this --> echo "<td><a href="../../article.php/'$id'/'$title'">$title<br /></a></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472023 Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 Same issue as I posted above, look at your quotes. You should review the fixes and apply them throughout your code. Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472024 Share on other sites More sharing options...
OreoKnight Posted February 20, 2008 Author Share Posted February 20, 2008 I just took them out.... And I get this.... Parse error: syntax error, unexpected $end in /home/jujubai/public_html/test6.php on line 170 There is nothing on that line.....? Its the end of the .php ??? Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472032 Share on other sites More sharing options...
OreoKnight Posted February 20, 2008 Author Share Posted February 20, 2008 Here is the full coding. <? include("inc/config.php"); $id = explode("/",$_SERVER['PATH_INFO']); $id = $id[1]; $sql = mysql_query("select * from categories where name='$id' limit 1") or die(mysql_error()); $row = mysql_fetch_assoc($sql); $title = $row['name']; $id = $row['id']; ?> <html> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Build Ur Body - Bodybuilding megasite for your muscle and fitness needs</title> <meta name="Description" content="Buildurbody.com is the ultimate resource for any bodybuilder or any person searching for fitness, getting better at sports, or know how to building muscle and losing fat"> <meta name="keywords" content="bodybuilding, muscle, fitness, diet, weight loss, exercise, your, articles, perfect body, weight lifting, athlete, plyometrics, health, lose fat, sports, nutrition, performance"> <link rel="StyleSheet" href="../../style.css" type="text/css" media="all" /> </head> <body> <div id="container"> <div id="head"> <div id="ad"> (Google ad) </div> </div> <div id="body"> <div id="left"> <div id="nav"> <? include("nav.php"); ?> </div> </div> <div id="nav_foot"></div> </div> <div id="right"> <div id="body_head">Articles Listings for <? print $title; ?></div> <div id="body_cont" style="font-size: 10px; font-family: verdana;"> <p> <br /> <div style="padding-left: 15px; font-size: 10px; font-family: verdana; margin-top: 15px;"><Center><b>Exercises</b></center><br/> <table border="2" bordercolor="#336699"style="padding-left: 15px; font-size: 10px; font-family: verdana; margin-top: 15px;" width="500"> <tr> <td> <img src="blank.jpg" height="150" width="200"></td> <td> <center><font size="3"><b>Chest</b></font><br/>The pec major and pec minor make up the region of the body known as the chest. Here is a comprehensive list of exercises that can be done to work these chest. In each page you will find the major muscles worked, the other muscles that help with the left, a description of the lift, the proper way to spot the lift, as well as the pros and cons of the lift.</td> </tr> </table><br/> <table cellspacing="3" cellpadding="3"> <?php $query = "select * from articles where cat_id='51' order by id DESC"; $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error()); if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { { $id = $row['id']; $title = $row['title']; // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product if($title != "" && $title != null) echo "<td><a href='article.php/'$id'/'$title'>$title</a></td>"; else{ echo "<td><a href=../../'article.php/'$id'/'$title'>$title</a></td>"; // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i < $max_columns) { for($j=$i; $j<$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> <br /> <br /> </p> </div> </div> </div> </div> <center> <br /> <br /> <div id="foot"></div> <? include("inc/foot.php"); ?> </center> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472035 Share on other sites More sharing options...
revraz Posted February 20, 2008 Share Posted February 20, 2008 Means you are probably missing a closing bracket somewhere. I just took them out.... And I get this.... Parse error: syntax error, unexpected $end in /home/jujubai/public_html/test6.php on line 170 There is nothing on that line.....? Its the end of the .php ??? Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472039 Share on other sites More sharing options...
OreoKnight Posted February 20, 2008 Author Share Posted February 20, 2008 I got it to actually display the page now..... but its not organizing the results into the columns and rows...... Three different columns, with each link in a column, and going to the next row when needed. You can goto www.buildurbody.com/test6.php to see what I mean. :'( Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472048 Share on other sites More sharing options...
OreoKnight Posted February 21, 2008 Author Share Posted February 21, 2008 Anyone got anything? Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472430 Share on other sites More sharing options...
sasa Posted February 21, 2008 Share Posted February 21, 2008 try <?php if($result && mysql_num_rows($result) > 0) { $i = 0; $max_columns = 3; while($row = mysql_fetch_array($result)) { $id = $row['id']; $title = $row['title']; // open row if counter is zero if($i == 0) echo "<tr>"; // make sure we have a valid product if($title != "" && $title != null) echo "<td><a href='article.php/$id/$title>$title</a></td>"; else { echo "<td><a href=../../'article.php/'$id'/'$title'>$title[/url]</td>"; } // increment counter - if counter = max columns, reset counter and close row if(++$i == $max_columns) { echo "</tr>"; $i=0; } // end if } // end while } // end if results // clean up table - makes your code valid! if($i > 0) { for($j=$i; $j<=$max_columns;$j++) echo "<td> </td>"; } ?> </tr> </table> ?> Quote Link to comment https://forums.phpfreaks.com/topic/92139-getting-query-results-to-print-into-a-table/#findComment-472762 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.