Jump to content

Getting query results to print into a table


OreoKnight

Recommended Posts

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!";

                                                                        }

                                                        ?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

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>

 

 

Link to comment
Share on other sites

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  ???

Link to comment
Share on other sites

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. :'(

Link to comment
Share on other sites

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>  
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.