Displaying content of associative arrayissue

I have the following php that selects data from 3 tables:

$result = mysqli_query($link, 'SELECT members.memberid, FName, LName, placing, eventName FROM members
INNER JOIN results ON members.memberid = results.memberid
INNER JOIN event ON results.eventid = event.eventid
WHERE members.memberid = 1');

while ($row = mysqli_fetch_array($result))
{
	$placings[] = array('id' => $row['memberid'], 'FName' => $row['FName'], 'LName' => $row['LName'], 'Event' => $row['eventName'], 'Placing' => $row['placing']);
}

In my html code I want to display the FName and LName once and then after that display the event and placing of this person.

I can use this code to display the event and placing but how do I display the FName and LName once only above this ?

<?php foreach ($placings as $place): ?>
        <div class="name_details">
        <p>Event:<?php echo($place['Event']); ?>, Placing: <?php echo($place['Placing']); ?></p>
        </div>
<?php endforeach; ?>

Echo it out before the foreach loop.
Though I fail to see the point of transferring the fetch array to another almost identical array, when you could just echo the results in the while loop.
I would only create a new array if I were altering or processing the data in some way.

Thanks for the replay. Call me a noob but HOW do you echo out the FName and LName before the foreach loop?

<h1><?php echo($place['FName']).' '.($place['LName']); ?></h1>
<?php foreach ($placings as $place): ?>
        <div class="name_details">
        <p>Event:<?php echo($place['Event']); ?>, Placing: <?php echo($place['Placing']); ?></p>
        </div>
<?php endforeach; ?>

Something like that. The html tags for the name can be whatever is appropriate, <h2>, <p> etc.

Thanks for the reply again but this doesn’t work since $place is not a defined variable until the foreach loop.

I did try previously putting

<?php echo($placings['FName']).' '.($placings['LName']); ?>

but then I get FName and LName as undefined index’s

That won’t work, because you’re building a two-dimensional array as $placings[], not a single-dimensional array. You would need to do something like:

<?php echo $placings[0]['FName'] . ' ' . $placings[0]['LName']; ?>

to access those fields for the first ‘row’ of the array. Obviously this only works if the value of those fields is the same for each row - if they might change, then alter your output loop to look for that change and output the new pair as needed.

1 Like

Success! Thanks so much for your help!

Sorry, that’s me copy/pasting without thinking :upside_down:

Edit @droopsnoot has it now.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.