Jump to content

[SOLVED] Showing links from the database one column keeps repeating


apotterdd

Recommended Posts

I've been at this for a couple of days now but can't seem to get my code to do what I want it to do.

 

$sql = "SELECT category, name, url FROM links ORDER BY category, name";

if ($result = mysqli_query($cxn,$sql))
while($row = mysqli_fetch_assoc($result)) 
{
	echo "<table cellpadding='5' cellspacing='0' border='1' bordercolor='#5F2A4C' align='center' width='75%'>
		  <tr><th>{$row['category']}</th></tr>" .
		 "<tr><td><a href='{$row['url']}' target='_blank'>" . 
         	 "{$row['name']}</a></td></tr></table>";
}	

 

It shows all the links just fine but I want it to keep from repeating the category name over and over.  The output I get is something like this.

 

Category

site url

 

Category

site url

 

Another Category

site url

 

Another Category

site url

 

It'll repeat that until it's run through all of the data in that particular table.  I want the category the links are in to only show up once and all of the links in that category to show up under it.

 

Does that make any sense?  I've tried nesting if statements and it gave me the same output as described above.

 

Any help is appreciated,

 

Anita

I've done the GROUP BY thing and it works but not as I expected.  It will only return one link in each category and not all of them.  I'm thinking it's got to be something with the table code but not sure where it's stopping at.

 

Is there something I'm missing in that code to return all rows for each category?

Yeah I would love to have links side by side instead of showing up one link per line.

 

I did find a tutorial on dividing rows into columns it last night but it confused me a lot.

 

I really appreciate the help I'm getting :)

If i understand you correctly,try like this:

1st echo your table outside while loop.

An example:

 

<table border='0'>
<tr>
<td>Category</td>
<td>URL</td>
</tr>
while($row=mysql_fetch_array($q)){
echo "<tr>";
echo "<td>$row['category']</td>";
echo "<td>$row['url']</td>";
echo "</tr>";
}
</table>

It's still not returning all of the links under the specified category.  I wanted to avoid making separate pages for three little sections and there isn't going to be that many links either.

 

I'll stare at it again tomorrow with fresh eyes.

I figured out a few hours ago that I needed more columns in my database as it kept returning all of the links instead of the ones I've specified.

 

I'm also looking to use the SELECT COUNT(columnname) so I can divide them evenly.  The two tutorials I did look at were not explanitory enough.  Just enough to know that I don't know enough.

 

Any definitive tutorials on this that is worded really well so that this relative noob can understand it?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.