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.

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.