0

This is an amateur question (so apologies in advance if im overlooking something really obvious but i've been hunting around PHP.net for some clues but havent figured it out yet), im deciphering this code someone else did for a friends site and kind of just got lost.

what the code is doing is creating an array for a database, and then the site is calling the information back based on time stamps and the sort. for some reason some things aren't showing up so i'm just trying to get my head through it:

$TYPES_EXPO = array("Type1","Type2","Type3","Type4","Type5","Type6","Type7");

$currentarray = $debe->runSql("SELECT * FROM expositions WHERE from<='$nu' AND to>='$nu' ORDER BY from");

if(count($currentarray)>0)
    echo "<h1>Current Exhibitions</h1>";

for($i=0; $i<count($currentarray);$i++)
{
    echo "<b>" . $currentarray[$i][3] . "</b>";

so in that last line, what is the [3] referencing? theres more of it in other parts of the code, different integers such as [6] and [7] in that similar configuration which seem arbitrary to me, just can't seem to get my finger on it…

1
  • do a var_dump($currentarray) and you'll see what's happening inside. looks like it's just an array dereference, selecting the 4th (0-based 3) column of a row of data in the result set. Commented Feb 12, 2013 at 16:23

2 Answers 2

1

I'd suspect that the call $debe->runSql is returning an array of records. the [3] is the 3rd column in the return.

Chances are it's the third column in your database table. But with SELECT * FROM there's no guarantee that the third column will be the same. You'll want to change it to SELECT first, second, third FROM

Again, this is assuming that $debe->runSql is returning something similar to http://php.net/manual/en/function.mysql-fetch-row.php

Sign up to request clarification or add additional context in comments.

Comments

0

The 3 is referencing the 4th element (0 is the first element) inside the multidimensional array $currentarray.

So for example you have an array $person

mike
    male
    blue
    75
    31
jack
    male
    brown
    80
    21

$person[1][3] would refer to the integer 21.

I hope this helps :)

//edit: just as a tip, don't use count() in the foreach. Assign a variable vefore so the function doesn't get called a thousand times when you have thousand array items.

2 Comments

Beat me to it. Note: you should change your sql from SELECT * FROM to SELECT column1, column2, column3 FROM so that the 3rd element is always the same even if the database table is changed.
Wouldn't [3] be accessing the fourth element?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.