Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print specific array entry using variable for position e.g. $array[$x]
    primarykey
    data
    text
    <p>With help from others on here, I've got a nested loop on the go that pull a list of months from one sql table and then, for each of those months, it goes through an events table and pulls the respective events.</p> <p>Table structures are along the lines of:</p> <p>MonthTable </p> <pre><code>ID | MonthShort | MonthLong 1 | 2012Oct | October 2012 2 | 2012Sep | September 2012 </code></pre> <hr> <p>EventTable </p> <pre><code>ID | MonthID | Event | Guests | Adults | Children 1 | 1 | Wedding | 200 | 150 | 50 2 | 1 | Bar Mitzvah | 100 | 50 | 50 3 | 1 | Funeral | 100 | 50 | 50 4 | 2 | Birthday | 50 | 30 | 20 5 | 2 | Birthday | 300 | 200 | 100 6 | 2 | Wedding | 200 | 180 | 20 </code></pre> <p>My loop works so that it populates menu A with all available months, then populates menu B with all of the events for that month. You can then click on the event and it displays the relevant information - this is where I'm a bit stuck.</p> <p>The arrays I've got are similar to the following, the guests array is what I'm trying out atm:</p> <pre><code>$events = array(); $months = array(); $guests = array(); while ($row = mysql_fetch_array($result)) { $months[$row["MonthID"]] = $row["MonthLong"]; $events[$row["MonthID"]][] = $row["Event"]; $guests[$row["MonthID"]][] = $row["Guests"]; } </code></pre> <p>I use a foreach to populate menu B with ($events[$x] as $event). The screen for each event will have an entry similar to the following and this is what I'd like to do (obviously I know this won't work bu it should serve for illustrative purposes):</p> <p>echo ' Number of guests: ' . print_r($guests[$x])</p> <p>With guests and events both on the same counter I though it would allow me to print the array entry in the relevant position.</p> <p>So what I'd like it if you click on "<code>October 2012</code>" and then select "<code>Funeral</code>", the screen would say:</p> <p><strong>Number of guests: 100</strong></p> <p>There are actually several dozen records per event but no point going into all of them...</p> <p>Apologies for the rambling and if this makes no sense! I'm new to PHP and am only really stuck on this bit.</p> <p>SQL query is built on the following:</p> <pre><code>$sql = " SELECT a.id, b.id AS monthId, a.event, b.monthshort, b.monthlong FROM events_table_name AS a INNER JOIN month_table_name AS b ON b.id = a.monthId ORDER BY b.id, a.id ASC "; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload