Jump to content

help with structuring PHP


essjay_d12

Recommended Posts

i have a database navlinks

[table][tr][td]c_id    [/td][td]name        [/td][td]p_id[/td][/tr][tr][td]1[/td][td]News[/td][td]0[/td][/tr][tr][td]2[/td][td]Headlines[/td][td]1[/td][/tr][tr][td]3[/td][td]Sport[/td][td]0[/td][/tr][/table]

I need the above table to print out like the following...

News
Sport

Once news is clicked it will reveal its sub categories identified by WHERE p_id = News c_id

so....

News
+  Headlines
Sport

There can be an unlimited amount of subcategories and this is where im having problems ... ie looping!!

anybody give me any hinters or help?

cheers

d
Link to comment
https://forums.phpfreaks.com/topic/27966-help-with-structuring-php/
Share on other sites

[code]
$qry = "SELECT * FROM `urtable` WHERE p_id = " . $_GET['c_id'] . " ORDER BY `name` ASC";
$qry = mysql_query($qry);
while($row = mysql_fetch_assoc($qry))
{
echo $row['name'];
}[/code]

Obviously you will have to put your own html in there to make it look right!!!
that works if there is only one sub category but it is unlimited so after a while the database could have been populated to look like the following.....

News
    Headlines
      Today
          World war 3!!!
          Nintendo console
          Virus hits all Computers
      Yesterday
Sport

etc

any thoughts?

this is what i got - but you said that if i made the next one a link it will work, however it doesnt....

here is my code....

it works to bring display the first sub category but when  i click on the sub category to reveal its own subcategory it only displays the main two categories!!

[code]<?php
session_start();
//open connection
$conn = mysql_connect("localhost", "admin", "adm1n");
mysql_select_db("links",$conn);

$query = "SELECT `c_id`, `name`, `p_id` FROM `navtest` WHERE p_id = 0";

$result = mysql_query($query) or die(mysql_error());
$num = mysql_num_rows($result);


if ($num > 0){

while ($row = mysql_fetch_array($result)){

$c_id = ($row['c_id']);
$name = ($row['name']);
$p_id = ($row['p_id']);

echo "<a href=navtest2.php?c_id=";
echo $c_id;
echo ">";
echo $name;
echo "</a>";
echo "<br />";

if ($c_id == $_GET['c_id']){

$qry = "SELECT * FROM `navtest` WHERE p_id = " . $_GET['c_id'] . " ORDER BY `name` ASC";
$qry = mysql_query($qry);

while($row = mysql_fetch_assoc($qry))

{

$c_id = ($row['c_id']);
$name = ($row['name']);
$p_id = ($row['p_id']);

echo "<a href=navtest2.php?c_id=";
echo $c_id;
echo ">";
echo $name;
echo "</a>";
echo "<br />";
//echo $row['name'];
//echo "<br />";
}
}

}

}


else {

echo "No links!!";
}
?>[/code]

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.