1

This is what i have

function GetEventsList(){
    $result = mysql_query("SELECT * FROM `events`") or trigger_error(mysql_error()); 
    while($row = mysql_fetch_array($result)){ 
    foreach($row AS $key => $value) { $row[$key] = stripslashes($value); } 
    $id = $row['id'];
    $en = $row['name'];
    $endt = $row['date'];
    $push = "<option value=$id>$en - $endt</option>";
    return $push;
    }
    }

Its only returning 1 result, when there are 3 in the table

3 Answers 3

4

That's because you're returning from the function at the end of the first iteration. Try:

function GetEventsList() {
  $result = mysql_query("SELECT * FROM `events`") or trigger_error(mysql_error()); 
  $push = '';
  while ($row = mysql_fetch_array($result)) {
    foreach($row AS $key => $value) {
      $row[$key] = stripslashes($value); 
    }
    $id = $row['id'];
    $en = $row['name'];
    $endt = $row['date'];
    $push .= "<option value=$id>$en - $endt</option>";
  }
  return $push;
}

On a side note: if you used good formatting/indenting that would've been far easier for you to spot.

Also, are you sure you need to use stripslashes()?

Sign up to request clarification or add additional context in comments.

1 Comment

Gotcha, how can i correct that to achieve the results im looking for?
2

If I'm reading the code correctly, return $push; is exiting the while loop prematurely. You want to return only after all results are gathered and stored in something.

Comments

0

Either use echo $push in your foreach loop; or put each iteration of the option element into an array, then insert the array as needed.

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.