tags:

views:

72

answers:

3

I am a little stuck on how to create this array.

My data:

category_id | category_name
     1              bikes
     2              cars
     3              books
     4              computers

Array:

$category=array();
$query = "SELECT * FROM categories ORDER BY name ASC";
$result = $db->query($query);
$category=array('category_id'=>$category_id, 'category_name'=>$category_name);
while ($row = $result->fetch_array()){
    $category_id=$row['category_id'];
    $category_name=$row['name'];
}

I want to create an array so that I can echo the data in a radio list like...

<input type='radio' value='<?PHP echo $category['category_id']; ?>' 
name='category[]'><?PHP echo $category['category_name']; ?>

o bikes
o cars
o books
o computers 

The problem is that the array only consists of one pair (1, bikes) and not all the data. How can I make an array with all the data?

Thanks!

+2  A: 

That is because you are echoing out side of the loop there by giving you only first row records eg bikes, you should do like:

$category=array();
$query = "SELECT * FROM categories ORDER BY name ASC";
$result = $db->query($query);
$category=array('category_id'=>$category_id, 'category_name'=>$category_name);
while ($row = $result->fetch_array()){
    $category_id=$row['category_id'];
    $category_name=$row['name'];
?>
<input type='radio' value='<?PHP echo $category['category_id']; ?>' 
name='category[]'><?PHP echo $category['category_name']; ?>
<?php
}
Web Logic
If you do `$row = $result->fetch_array()` then the `$row` array contains all data if you check it with `print_r($row);`
Web Logic
@jpjp: You are welcome.........
Web Logic
+3  A: 

You are doing three things wrong, first you are not actually setting $category to equal anything - only the values of two undefined values, which default to null. So you end up with an array like:

array('category_id' => null, 'name' => null)

Second, you are doing nothing with the values of $category_id and $category_name when you do set them. Next, you are not going through the array item by item, you simply output the initial way it was set - which is linked to another problem that your array is short of a dimension. It should be 2-dimensional, not 1-dimensional.

This code sums up the gist of it all. I would suggest reading up on how arrays work in PHP and how to define them, they're a very commonly used data type throughout frameworks and the like.

$query = "SELECT * FROM categories ORDER BY name ASC";
$result = $db->query($query);

$categories=array();

while ($row = $result->fetch_array()){
    $categories[] = array('category_id' => $row['category_id'], 'category_name' => $row['name']);
}

Then when you need to output:

foreach ( $categories as $category ) {
    ?>
    <input type='radio' value='<?php echo $category['category_id']; ?>' name='category[]'><?php echo $category['category_name']; ?>
    <?php
}

This can of course be cleaned up further.

Geoff Adams
you sir are the man! thank you, I was trying to do it this way,but blanked out on the foreach loop.
jpjp
Is it possible to do this with array_push if I have keys? Because the first parameter has to be an array, but say I want it to be in the 'category_id' and not [0],[1],etc
jpjp
I'm not sure what you mean - the syntax $categories[] is equivalent to array_push().
Geoff Adams
Furthermore, with this structure you have this: array( 0 => array('category_id' => ..., 'category_name' => ...), 1 => array('category_id' => ..., 'category_name' => ...), ... )
Geoff Adams
yes your way is much more cleaner. I wasn't sure what I was doing..
jpjp
+1  A: 

You are assigning to variables $category_id and $category_name, rather than to the array $category, so you're only seeing the first row returned from the query.

I normally use PDO, and would write it something like this:

<?php
  $db = ...
  $sql = 'SELECT * FROM categories ORDER BY category_name ASC';
  $categories = $db->query($sql);
?>

<html>
<body>
<? foreach ($categories as $category): ?>
<input type="radio" name="category[]" value="<?= $category->category_id ?>">
  <?= $category->category_name ?>
</input>
<? endforeach ?>
</body>
</html>
ChrisV