0

I am currently trying to create a ticketing system from scratch using PHP and MYSQL. I would like to have a drop down that allows you to assign tickets to a user based on the user list from MYSQL. Currently this is the code I have to populate the with options:

$sqlq1 = "SELECT fname, lname FROM ticket_users";
$query = mysql_query($sqlq1);
$array = mysql_fetch_array($query);


foreach($array as $row){
echo '<option value ='."$row[fname]".'>'.$row[fname].$row[lname].'</option>';
}

The issues I'm having are three fold:

  1. The code only prints from the first row of the SQL
  2. The code does not cycle equal to the number of user records
  3. When the code does print the information from the first row it's only printing the first characters from each field and is multiplying them.

The output looks like this:

<h3> Assign To: <select name="assignee">
<option value =J>JJ</option>
<option value =J>JJ</option>
<option value =F>FF</option>
<option value =F>FF</option>

</select></h3>

And this is what a print_r of the array looks like:

Array ( [0] => Jim [fname] => Jim [1] => Frail [lname] => Frail )

And Here's the var dump:

array(4) { [0]=> string(3) "Jim" ["fname"]=> string(3) "Jim" [1]=> string(5) "Frail" ["lname"]=> string(5) "Frail" }

I wasn't even sure how to search for a solution to this issue so hopefully my title will help some other people find the solution as well.

Thanks in advance for the help.

PS. After some looking in to it I have been able to determine that the J's all come from the first name of the first row and all the F's come from the last name of the first row. Hope this helps.

9
  • 3
    opton <= a typo. Plus, you should quote your values. Commented May 1, 2015 at 18:48
  • Thanks for the catch on that! Unfortunately doesn't seem to be what was causing the echo to get confused. Commented May 1, 2015 at 18:50
  • Also, you're using mysql_ functions which are deprecated and will be removed in PHP 7 Commented May 1, 2015 at 18:50
  • update your question then in order to reflect it. Commented May 1, 2015 at 18:50
  • Write var_dump($array); above the foreach loop [Debugging purposes only] Commented May 1, 2015 at 18:50

3 Answers 3

1

Quotes can be very confusing, I always prefer to set the variables outside the echo block. The while loop saves some code.

Try this instead:

$sqlq1 = "SELECT fname, lname FROM ticket_users";
$query = mysql_query($sqlq1);

while($row = mysql_fetch_array($query)){
 $fname =  $row['fname']; // you missed the single-quotes
 $lname =  $row['lname'];
echo "<option value ='$fname'>$fname $lname</option>";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that makes it much easier to read. I didn't realize that the query didn't just pull the whole column all at once.
0

You need a while loop top iterate over your recordset. This way you pull one record per row and echo that

$sqlq1 = "SELECT fname, lname FROM ticket_users";
$query = mysql_query($sqlq1);

while($row = mysql_fetch_array($query)){
    echo '<option value ="' . $row['fname'] . '">' . $row['fname'] . $row['lname'] . '</option>';
}

1 Comment

Indeed, $array = mysql_fetch_array($query); is only fetching the first one I believe.
0

mysql_fetch_array fetches a result row as an associative array, a numeric array, or both. If you want to fetch all records returned by query you need to use while loop similar to:

 while($array = mysql_fetch_array($query)){
    echo '<option value ='."$array[fname]".'>'.$array[fname].$array[lname].'</opton>';
    }

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.