1

I want to return cats value in select options and cats id as option id.I am successfully returning cats values as follows.

if($nu) {

                $cat_name = array();

                while ($row = mysqli_fetch_array ($sq2)) {
                    $cat_name[] = $row['cat_name'];
                }
                return $cat_name;

            }

But I want to return both cats values and their ids in option tag.I tried this but failed .Please help

if($nu) {

                $cat_name = array();
                $cat_id = array();

                while ($row = mysqli_fetch_array ($sq2)) {
                    $cat_name[] = $row['cat_name'];
                    $cat_id[] = $row['id'];
                }
                  return "<option id='$cat_id'>$cat_name</option>";

            }
2
  • Your variables are arrays, so you need an index. Do you want to return just one cat or all of them? Commented Nov 20, 2013 at 16:36
  • @HanletEscaño I am using while loop,That means i needed all the related rows Commented Nov 20, 2013 at 16:42

3 Answers 3

3

Try like

$html = '';
while ($row = mysqli_fetch_array ($sq2)) {
    $cat_name = $row['cat_name'];
    $cat_id = $row['id'];

    $html .= "<option id='".$cat_id."'>".$cat_name."</option>";
}
return $html;
Sign up to request clarification or add additional context in comments.

Comments

0

Something more like this, need to add each line as you go

if($nu) {


$option = "";

while ($row = mysqli_fetch_array ($sq2)) {
   $option.= <option id="$row['id']">$row['cat_name']</option>
}
 return $option;

}

Comments

0

You want to return 1 option by cat ?

Your need to concat your HTML in the loop.

if($nu) {

            $html = "";

            while ($row = mysqli_fetch_array ($sq2)) {
                $html .= "<option id='".$row['id']."'>".$row['cat_name']."</option>"
            }
            return $html;

        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.