PDA

View Full Version : problem understanding array results


songboy
10-22-2010, 02:10 PM
I have created a test associative array which looks like this:
writer_id => 9 second_name => Hur first_name => Ben
writer_id => 11 second_name => Hur first_name => Fred
What I am trying to do is access the element writer_id => 9 by referring to its position within the array. Here's the code to show what I did.

$query = ("SELECT writer_id, writing_first_name, writing_second_name FROM writer_page_details WHERE writing_second_name LIKE '$letter_chosen%' ORDER BY writing_second_name");
$result1 = mysqli_query($cxn, $query);
@$names = mysqli_fetch_assoc($result1);
$_SESSION['names'] = $names;
$names = $_SESSION['names'];
mysqli_data_seek($result1,0);
while($names = mysqli_fetch_assoc($result1))
{
$id = array($names['writer_id']);
for(@$row = 0; $row < count($names['writer_id']); $row++)
{
print_r($id);
}
}

When I do this, the display shows as follows:
Array ( [0] => 9 ) Array ( [0] => 11 )
Why is it that two separate writer_id results have the same key ? To me it's saying the results are in the same row. As such, it seems it would not be possible for me to access the first id separately because both ids are one and the same thing. I've played around with print_r call and get a really odd display. If I call this:

print_r($id[0][0]);

I get this 91 - the second value eg 11 has been split and joined with the first value !!!
Could someone explain what's going on ? It should be possible for me to access a single value by pointing to its position in the array.
Help would be gratefully received - thanks.
Songboy

davidj
10-23-2010, 07:47 PM
dont use a print_r in the loop

davidj
10-23-2010, 07:49 PM
also

you have this...

@$names = mysqli_fetch_assoc($result1);
$_SESSION['names'] = $names;
$names = $_SESSION['names'];

loose the @ suppressor and error handle

omit the last line as its not needed

songboy
10-23-2010, 10:22 PM
Thanks David for your help.
I stayed up last night for a long time trying to understand what exactly it was I was trying to do. After some searching I realized that I was trying to make a multi-dimensional array with an associative array at its centre. Basically, I needed to create rows out of the data which were actually rows ie accessible manually. This is how you do it.
Given that you've created an associative array, the following code will break up the array into chunks. Each chunk has a numerical first key which basically counts the chunks. The following key/value pair point to the headings within your associative array eg writer_id => 9. What I did was to manipulate my code in order to use the first numerical key as a pointer to a table entry in a list of writers' names. Have a look:

mysqli_data_seek($result1,0);
$i=1;
$ret = array();
while ($row = mysqli_fetch_assoc($result1))
{
foreach ($row as $key => $value)
{
$ret[$i][$key] = $value;
}
$i++;
}
@$got_id = $ret[@$chosen_row]['writer_id'];

PS The first line is essential because you need to return the loop pointer back to the beginning before you start a new pass.
Now I reckon that's worth a promotion !