-1

I am having trouble with a simple problem. I googled it and found so many resource but nothing is useful in my perspective.

Here I ran a query and output it as a select menu. But first row of the value isn't show up.

<?php
$query = mysql_query("SELECT * FROM `subcategory` ORDER BY `Category_id`");             
while($row = mysql_fetch_array($query)){
$subcategory_title = $row['Subcategory_name'];
$subcategory_id = $row['Subcategory_id'];
$subcategory_id = $subcategory_id*10;

$selectMenu .= "<option value='$subcategory_id'>$subcategory_title</option>";
}
?>

and output is in the

<select name='' class=''>
<option disabled='disabled'>Choose a Topic</option>
<?php echo $selectMenu; ?>
</select>

I am echo out the $subcategory_title and $subcategory_id, here first row value show up whether in list menu it is not. Same code in another script works fine. I don't understand. What is the problem?

Hugo Dozois
  • 7,807
  • 12
  • 51
  • 57
Nabil
  • 317
  • 9
  • 26
  • Did you get all the values when you run only the sql in the mysql? – sel Apr 19 '13 at 03:09
  • 1
    [Please, don't use `mysql_*` functions](http://stackoverflow.com/q/12859942/1190388) in new code. They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the red box? Learn about prepared statements instead, and use [tag:PDO] or [tag:MySQLi]. – hjpotter92 Apr 19 '13 at 03:10
  • I can't tell you the reason for sure, but it seems like you should put your $subcategory_id = $subcategory_id*10; at the end of the loop – apoq Apr 19 '13 at 03:24
  • +1 to @hjpotter92 spread the word! – Rixhers Ajazi Apr 19 '13 at 03:45
  • Hello! There is no need to put "solved" in the title since the "check" system is already doing that job! For more information see http://meta.stackexchange.com/questions/172501/community-edit-request-solved-fixed-answered – Hugo Dozois Apr 27 '13 at 03:01

2 Answers2

0

Code Link : http://pastebin.com/iGqitNe7

Try this...

<?php

 $qidInfo = "<tr>
                <td width='20%'>Exam Topic:</td>
                <td width='80%'><label>
                <select class='exam_name' name='exam_name' id='exam_name'>
                <option disabled='disabled'>Choose a Topic</option>".$selectMenu."
            </select>...rest of the string...";
?>
Jay Bhatt
  • 5,485
  • 4
  • 35
  • 60
  • It's not it...I am already initialize $selectMenu on the top of the code...Here I am just showing part of my code. – Nabil Apr 20 '13 at 04:49
  • 1
    Try this... change this to "string.....".$selectMenu."rest of the string..." – Jay Bhatt Apr 20 '13 at 07:39
0

Why not do it the RIGHT way with PDO like so? : (might have some syntax errors untested btw)

$sql = "SELECT * FROM `subcategory` ORDER BY `Category_id`";
$query = $pdo->prepare($sql);
$query->execute();

if($query) {
   $row = $query->fetchAll(PDO::FETCH_ASSOC);
   $selectmenu = "";
   foreach ($row as $value) {
        $selectMenu .= "<option value=<?php echo $value['subcategory_id']*10 ?>><?php echo $value['subcategory_title'] ?></option>";

    }
}

This is untested code but I think this should work. Good luck post any concerns you might have btw.

For help setting up PDO refer to this answer. Your asking yourself why use PDO? Well as stated in the comments above Mysql_ functions should be avoided at all costs for new code as they are looked down by the PHP community and will be removed as of php 5.5.

Read more on PHP PDO here

Community
  • 1
  • 1
Rixhers Ajazi
  • 1,283
  • 11
  • 18