Jump to content

need help to code list menu with categories


pixeltrace

Recommended Posts

hi,

 

i need help, i need to develop a list menu which have category and subcategory

the html code looks like this

<select name="select">
<option value="keyword"><b>category 1</b></option>
<option value="keyword">subcategory1a</option>
<option value="keyword">subcategory1b</option>
<option value="keyword"><b>category 2</b></option>
<option value="keyword">subcategory2a</option>
<option value="keyword">subcategory2b</option>
</select>

 

but items for this list menu will be taken from the database

my table fields are

id | parent | keyword | name |

1  | 0        | keyword | category1

22 |  1  | keyword | category1a

23 | 1  | keyword | category1b

2 | 0 | keyword | category2

24 |2 | keyword |category2a

25 | 2 | keyword | category2b

 

how will i generate the query so my outout will look similar to

the html code?

 

hope you could help me with this.

 

thanks!

 

 

Link to comment
Share on other sites

Hi there,

 

I think I understand you correctly; you want to populate a drop-down list with data from the db? If so, this will put you on the right track:

 

<select name="select">

<?php
require_once('config.php'); // Contains DB info and connection
$sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need
while($row = mysql_fetch_array( $sql ))
{
$id = $row['id'];
$parent = $row['parent'];
$keyword = $row['keyword'];
$name = $row['name'];
?>

<option value="<?php echo $keyword ?>"><b><?php echo $name ?></b></option>

<?php
}
mysql_close($con); // Close the connection as specified in config.php
?>

</select>

 

I haven't tested the above code yet and you may have to modify it some to get it working for you. I hope it works!

 

Regards,

Iceman

Link to comment
Share on other sites

hi,

 

thanks this is what i actually did on my php code,

but what i want is that the list menu will be in order by category

my current code is like this

                                    <select name="poitype" class="style2" style="width:200px">
								<option value="">--select one--</option>
		<?
		$uSql = "SELECT code, provider_name, id, parent FROM jos_findit_product_service_providers ORDER by provider_name ASC";
		$uResult = mysql_query($uSql, $connection);
		if(!$uResult){
		echo 'no data found';
		}else{
		while($uRow = mysql_fetch_array($uResult)){
		$code = $uRow['code'];
		$provider = $uRow['provider_name'];
		$parent = $uRow['parent'];
		$id = $uRow['id'];
			?>
            <? if ($parent = '0'){
		echo "<option value=\""."$code"."\"><b>"."$provider"."</b></option>";
		}elseif ($parent !='0' && $id == '$parent'){
		echo "<option value=\""."$code"."\">"."$provider"."</option>";
			}
			}    
			}
			?>		

                                      </select>

 

but currently its returning me nothing.

 

this is how my table in my DB looked like

id | parent | keyword | name |
1  | 0        | keyword | category1
22 |  1  | keyword | category1a
23 | 1  | keyword | category1b
2 | 0 | keyword | category2
24 |2 | keyword |category2a
25 | 2 | keyword | category2b

 

hope you could help me with this.

 

thanks!

Link to comment
Share on other sites

Hi again,

 

The code I have written should order the drop-down list by category as you require. The key to arranging the data by category is here:

... ORDER BY name DESC");

 

Please copy this modified piece of code to your page and try it.... it should work  :)

<select name="select">
<option value=""><b> ----- PLEASE SELECT ONE ----- </b></option>

<?php
require_once('config.php'); // Contains DB info and connection
$sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need
while($row = mysql_fetch_array( $sql ))
{
$id = $row['id'];
$parent = $row['parent'];
$keyword = $row['keyword'];
$name = $row['name'];

echo '<option value="'.$keyword.'"><b>'.$name.'</b></option>';
}
mysql_close($con); // Close the connection as specified in config.php
?>

</select>

 

NOTE: You say that the columns in the DB are id, parent, keyword and name. However in your code, the columns are code, provider_name, id, parent. Is this intentional?

 

Let us know if it works!

Iceman

Link to comment
Share on other sites

Hi again,

 

Ok.... sorry, I did misunderstand you. Well, let us try the following:

 

<select name="select">
<option value=""><b> ----- PLEASE SELECT ONE ----- </b></option>

<?php
require_once('config.php'); // Contains DB info and connection
$sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need
while($row = mysql_fetch_array( $sql ))
{
$id = $row['id'];
$parent = $row['parent'];
$keyword = $row['keyword'];
$name = $row['name'];

  if ($parent = '0' || $parent == '0'){
    echo '<option value=""><b>» » '.$name.'</b></option>';
  }
  elseif ($parent != '0' || $parent !== '0'){
    echo '<option value="'.$keyword.'">'.$name.'</option>';
  }
  else {
    echo 'An uknown error has occured.';
  }

}
mysql_close($con); // Close the connection as specified in config.php
?>

</select>

 

I hope that sets you on the right path. Again, I haven't tested the above code yet.

 

Regards,

Iceman

Link to comment
Share on other sites

Hi,

 

It will have to have the following:

 

<?php
/* ----- DB CONFIGURATION ----- */
$dbhost = 'host';
$dbuser = 'user';
$dbpass = 'password';
$dbname = 'database';

/* ----- DB CONNECTION ----- */
$con = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db($dbname, $con);
?>

 

Just use it as an include.  :)

 

Iceman

Link to comment
Share on other sites

hi,

 

this code is working.

but the only thing that is missing is that the subcategories are not in line with its main category

kindly check my test site

http://mango.resonance.com.sg/myoochi/findit/providerfindit2.php

click on advanced search tab

click 'search by product & service providers'

and see what i mean.

 

the order should be something like this

http://mango.resonance.com.sg/myoochi/findit/testa.html

 

hope you could help me with this.

 

thanks!

Link to comment
Share on other sites

Hi there,

 

Ok... give the following a try and see if it works.

 

<select name="select">
<option value=""><b> ----- PLEASE SELECT ONE ----- </b></option>

<?php
require_once('config.php'); // Contains DB info and connection
$i='0'
$sql = ("SELECT * FROM table ORDER BY name DESC"); // For speed, only pull the rows you need
while($row = mysql_fetch_array( $sql ))
{
$id = $row['id'];
$parent = $row['parent'];
$keyword = $row['keyword'];
$name = $row['name'];

  if ($parent = '0' || $parent == '0'){
    echo '<option value=""><b>» » '.$name.'</b></option>';
$i++
	if ($parent = $i || $parent == $i){
    	echo '<option value="'.$keyword.'">'.$name.'</option>';
   		}
	else {
	}
  }

}
mysql_close($con); // Close the connection as specified in config.php
?>

</select>

 

Let's hope so....

 

Iceman

Link to comment
Share on other sites

hi,

 

thanks for the response again.

i tried the codes you gave me,

but its returning 0 results

this is my current code

                                    <select name="poitype" class="style2" style="width:200px">
								<option value="">--select one--</option>
		<?

$i='0';
$sql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need
while($row = mysql_fetch_array( $sql ))
{
$id = $row['id'];
$parent = $row['parent'];
$code = $row['code'];
$name = $row['provider_name'];

  if ($parent = '0' || $parent == '0'){
    echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>';
$i++;
	if ($parent = $i || $parent == $i){
    	echo '<option value="'.$code.'">'.$name.'</option>';
   		}
	else {
}
  }
}
mysql_close($connection); // Close the connection as specified in config.php
?>		



                                      </select>

 

whatelse did we miss?

hope you could help me with this.

 

thanks!

Link to comment
Share on other sites

hi,

 

i tried this

                                    <select name="poitype" class="style2" style="width:200px">
								<option value="">--select one--</option>
		<?

$i=0;
$uSql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need
		$uResult = mysql_query($uSql, $connection);
		if(!$uResult){
		echo 'no data found';
		}else{
		while($uRow = mysql_fetch_array($uResult)){
			$id = $uRow['id'];
			$parent = $uRow['parent'];
			$code = $uRow['code'];
			$name = $uRow['provider_name'];

  if ($parent = 0 || $parent == 0){
    echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>';
$i++;
	if ($parent = $i || $parent == $i){
    	echo '<option value="'.$code.'">'.$name.'</option>';
   		}
	else {
}
}
}
}
mysql_close($connection); // Close the connection as specified in config.php
?>		

 

but its only returing the main category

but not the subcategory

in my database

 

the

parent of my subcategory is equal to the id of the main category

and main category has a 0 value for the parent

 

id | parent | code | provider_name |

1      0        wer    werer

2      0        rty    rtytyt

4      1        tytu    rtyttut

5      2        erere  rtrtrtrtr

 

 

something like that.

check my test site

http://mango.resonance.com.sg/myoochi/findit/providerfindit2.php

click on advance tab and on that page, click 'search for product & service providers'

you will see the returned value.

hope you could help me fix the code

 

thanks!

Link to comment
Share on other sites

try

echo'<select name="poitype" class="style2" style="width:200px">
<option value="">--select one--</option>';
$i='0';
$sql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need
while($row = mysql_fetch_array( $sql ))
{
$id = $row['id'];
$parent = $row['parent'];
$code = $row['code'];
$name = $row['provider_name'];
if ($parent == '0'){
	echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>';
}
if ($parent == $i){
	echo '<option value="'.$code.'">'.$name.'</option>';
}
$i++;
}
echo ' </select>';
mysql_close($connection);

note not tested<----------

Link to comment
Share on other sites

hi,

 

i tried using this code

			<?

$i=0;
$uSql = ("SELECT * FROM jos_findit_product_service_providers ORDER by provider_name ASC"); // For speed, only pull the rows you need
		$uResult = mysql_query($uSql, $connection);
		if(!$uResult){
		echo 'no data found';
		}else{
		while($uRow = mysql_fetch_array($uResult)){
			$id = $uRow['id'];
			$parent = $uRow['parent'];
			$code = $uRow['code'];
			$name = $uRow['provider_name'];

  if ($parent == '0' || $parent == '0'){
    echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>';
$i++;
	if ($parent == $i || $parent == $i){
    	echo '<option value="'.$code.'">     '.$name.'</option>';
   		}
	else {
}
}
}
}
mysql_close($connection); // Close the connection as specified in config.php
?>		

but its only showing me the main category

is there aways that the logic on the second condition will be like this

$parent ='0';
              echo '<option value="'.$code.'"><b>» » '.$name.'</b></option>';
$parent !=0 and $id = maincategory id;
     	echo '<option value="'.$code.'">     '.$name.'</option>';

 

hope you could help me fix this code because i really cant visualize how

the could flow will be.

 

thanks!

 

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.