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
Share on other sites

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?

Link to comment
Share on other sites

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]
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.