Jump to content

[SOLVED] Questions about select tag embeded with php code


andz

Recommended Posts

How to make the select tag view content like this?

 

Desktop

-Hardware

-Software

Health

-Good tips

-Food Poisoning

Security

Guides

 

 

All of the records above are in database format.

 

category_table

 

catid,    name,              parentid

 

1          Desktop              0

2          Hardware            1

3          Software            1

4          Health                0

5          Good tips            4

6          Food Poisoning      4

7          Security              0

8     Guides              0

 

 

I'm also wondering if where is this implemented? Upon query or on the select tag itself.

Please include code if possible.. Thanks.

 

 

 

$sql = "SELECT * FROM categories WHERE parentid = 0;";
$query = mysql_query($sql);


while($row = mysql_fetch_assoc($query)) {

   echo $row['name'];
       $sql = "SELECT * FROM categories WHERE parentid =".$row['catid'].";";
       $query = mysql_query($sql);
       while($rows = mysql_fetch_assoc($query)) {
            echo "-".$rows['name'];
       }

}

 

There's my solution.  IT's crap, but meh.

Thanks for the solution. I'll give it a try. Thanks.

 

Hmm, I wouldn't, it'll crash if it can't find anything to put into mysql_fetch_assoc..

 

<?php

$sql = "SELECT * FROM categories WHERE parentid = 0;";
$query = mysql_query($sql);

while ($row = mysql_fetch_assoc($query))
{
    echo $row['name'];

    $sql = "SELECT * FROM categories WHERE parentid =" . $row['catid'] . ";";
    $query = mysql_query($sql);

    if (mysql_numrows($query) > 0)
    {
        while ($rows = mysql_fetch_assoc($query))
        {
            echo "-" . $rows['name'];
        }
    }
}

?>

 

There, that should fix it.

Hi.

 

you can use the following query to get reocrds with cat and sub cat, suppose ur table name "cat1"

and having ur sample data       

 

      select newCatTab.name as maincat,cat1.name as subcat from cat1

      right join

            (select name,catid from cat1 where parentid=0) as newCatTab

    on

            (newCatTab.catid = cat1.parentid)

 

 

will result in

____________________

maincat  | subcat

____________________

desktop  | software

desktop  | hardware

health    | Good

...

 

 

Regards,

Vijay

Ive modified your code and it worked.

 

Here's the modified one:

 

<select name="select">

<?php

 

$sql = "SELECT * FROM test WHERE parentid = 0;";

$query = mysql_query($sql);

 

while ($row = mysql_fetch_assoc($query))

{

?>

<option value="#"><?=$row['name']?></option>

<?php

$catid = $row['catid'];

 

$result = mysql_query("SELECT * FROM test WHERE parentid='$catid'");

if (!$result) {

die(mysql_error());

} else {

$res = array();

if (mysql_num_rows($result)) {

while ($row1 = mysql_fetch_assoc($result)) {

$res[] = '<option value="#">'.'-'.$row1['name'].'</option>';

}

}

echo implode('', $res);

}

 

//    $sql = "SELECT * FROM test WHERE parentid =" . $row['catid'] . ";";

//    $query = mysql_query($sql);

 

//    if (mysql_num_rows($query) > 0)

//    {

//        while ($rows = mysql_fetch_assoc($query))

//        {

//            echo "-" . $rows['name'];

//        }

//    }

}

 

?>

</select>

 

 

 

Thanks for all your replies.... GodSpeed

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.