Hi,
I’m trying to return a option down down box for a section where the user can edit a form from the data returned. For the drop down box I want to build an array of values returned from the database and match the value that is in the database against the list of values and add a selected attribute to it. The code I’m using below is flawed because I’m trying to build a an array with a loop inside and it’s not working. Any tips?
public function returnNewsItemDropDownMenuForEdit($id) {
$result = $this->mMysqli->query('SELECT * FROM news WHERE live = 1 ORDER BY date DESC');
$row = $result->fetch_array(MYSQLI_ASSOC);
if ($row == 0) {
echo '<option value="">No Entries Found</option>';
} else {
$story = array(
do {
array('value' => $row['id'], 'label' => $row['title']),
} while ($row = $result->fetch_array(MYSQLI_ASSOC))
);
foreach($story as $k=>$v) {
$result = $this->mMysqli->query('SELECT * FROM news WHERE id = $id');
$row = $result->fetch_array(MYSQLI_ASSOC);
if ($row['id'] == $v['value']) {
echo '<option selected="selected" value="'.$v['value'].'">'.$v['label'].'</option>'. "\
";
}
else
{
echo '<option value="'.$v['value'].'">'.$v['label'].'</option>'. "\
";
}
}
Whoa, does any of that even work ?
At a glance, it looks like you want a drop down list of your news titles, with the value attribute being the id of the news story? Is that right?
Then the id your specifying, this should be the one that is marked as selected?
Your method should only ‘return’ data not output it, here’s modified version that just returns an array of news items.
<?php
class News
{
/* @var mysqli */
protected
$db;
public function __construct(Mysqli $db){
$this->db = $db;
}
public function find($criteria = array()){
/* @var mysqli_result */
$result = $this->db->query(
'SELECT `id`, `title` FROM news WHERE `live` = true ORDER BY `date` DESC;'
);
$news = array();
if($result){
while($item = $result->fetch_assoc()){
array_push($news, $item);
}
}
return $news;
}
}
?>
We can then call News::find() to get all news items from the database, from that point on, we just need to build the drop down menu with the specified option selected.
<?php
function news_select($items, $selected_id){
$select = '<select name="news">';
foreach($items as $item){
$select .= sprintf(
'<option value="%d"%s>%s</option>',
$item['id'],
$selected_id == $item['id'] ? 'selected="selected"' : '' ,
$item['title']
);
}
$select .= '</select>';
return $select;
}
?>
Something like that should do the job.
So, the general feel would be something a kin to…
<?php
$news = new News($db);
echo news_select($news->find(), 5);
?>
All untested and probably no chance of working.

Thanks for the earlier tip.
While I saw your response I was trying the below which should work but no data is pulling through at all.
public function returnStoryId($id) {
$query = $this->mMysqli->query('SELECT story_id FROM header WHERE id = $id');
$selected = $query->fetch_array(MYSQLI_ASSOC);
return $selected['story_id'];
}
public function returnNewsItemDropDownMenuForEdit($id) {
$result = $this->mMysqli->query('SELECT * FROM news WHERE live = 1 ORDER BY date DESC');
$row = $result->fetch_array(MYSQLI_ASSOC);
$story_id = $this->returnStoryId($id);
if ($row == 0) {
echo '<option value="">No Entries Found</option>';
} else {
echo '<option value="">Please Select a Story</option>';
do {
echo '<option';
if($story_id == $row['id']) echo 'selected="selected"';
echo 'value="'.$row['id'].'">'. $row['title'].'</option>';
} while ($row = $result->fetch_array(MYSQLI_ASSOC));
}
}
if ($row == 0)
Ew. Try something like… $result->num_rows == 0, and turn your while loop right way round.
anyway. Standard “It didnt work” mysql response: Have you tried the queries in your database engine to make sure they actually pull out the data you’re expecting?
secondary response: Your ID getter wont work - single-quoted strings dont expand their variables. (‘SELECT story_id FROM header WHERE id = $id’)
$test = "turkey";
echo 'I am a $test'; // I am a $test
echo "I am a $test"; // I am a turkey