Welcome to WebmasterWorld Guest from 54.160.131.144

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Arrays

Populating Arrays

     

MajorPat

7:28 pm on Apr 6, 2010 (gmt 0)

5+ Year Member



First, I am brand new to PHP.

I have used a converter to build some PHP and trying to understand what it is doing.

I have this block of code:

array("lstInstallations",74,62,419,218, 110,array( 4,"","SELECT
tblbaseinstlncodes.state,
(CASE WHEN InstlnCode = ParentCode THEN 'P' ELSE ''END) AS PID,
tblbaseinstlncodes.instlnname,
tblbaseinstlncodes.instlncode
FROM
public.tblbaseinstlncodes
Order BY state; ",0,4 ))

I understand that it is populating an array from my select statement to then populate a select box on my form. But it only displays one field where the select statement is pulling 4 fields.

Thoughts?

Pat

Matthew1980

10:08 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there MajorPat,

Welcome to the forum!

Are you saying that this code is functional, and you are just needing to understand how its doing it?

All as I can say is from a quick look is that you are using a multidimensional array (Not my strong suit unfortunately), and the query that's inside it looks like it's calling the data from 1 table, but as far as I can see, there is no stipulation to say LIMIT 4 ie, only pull four records from the available data in the table, except if:-

Order BY state; ",0,4 ))

the quoteation was the other side of the four, then this would be true, as you would be saying give me 4 records from position 0, as all records start at 0.

Either that or I have misinterpreted the code.

If you are having a problem displaying the results, are you displaying them from inside a while loop at all, as the data retrieved needs a loop to iterate though each instance returned.

Hope I have understood you correctly,

Cheers,
MRb

Readie

10:49 pm on Apr 6, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



array(
[0] => "lstInstallations",
[1] => 74,
[2] => 62,
[3] => 419,
[4] => 218,
[5] => 110,
[6] => array(
[6][0] => 4,
[6][1] => "",
[6][2] => "SELECT tblbaseinstlncodes.state, (CASE WHEN InstlnCode = ParentCode THEN 'P' ELSE ''END) AS PID, tblbaseinstlncodes.instlnname, tblbaseinstlncodes.instlncode FROM public.tblbaseinstlncodes Order BY state;",
[6][3] => 0,
[6][4] => 4
)
)

Split it up and look at it like that (if only I could tab in on these forums)

A multi dimensional array of values, one of which is a SQL statement

bizminder

7:54 am on Apr 7, 2010 (gmt 0)

5+ Year Member



Thanks readie, thats the whole purpose of multi dimensional arrays to hold values of different types.

MajorPat

1:35 pm on Apr 7, 2010 (gmt 0)

5+ Year Member



thanks folks, this does help. The select statement is pulling 4 fields from the database and 40 records. Unfortunately, [6][3] can only hold one piece of information, therefore, I will need 4 select statements. Pretty cool... I'll give it a whirl.

MajorPat

2:04 pm on Apr 7, 2010 (gmt 0)

5+ Year Member



One last silly question. Does the => sign mean "assigned"? I can't find it in any of my references books.

Readie

2:07 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



I was just using that as a means of displaying the code in an easy-to-read manner.

What I wrote is invalid PHP coding.

The => sign is actually used for assigning in arrays, however:

$some_array = array(
0 => 'Hello',
1 => ' ',
2 => 'world'
);

However it's generally used when you actually want a custom name on the value's key, like

$some_array = array(
'first' => 'Hello',
'second' => ' ',
'third' => 'world'
);

echo $some_array['first']; // Outputs Hello

Matthew1980

2:20 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there MajorPat,

Readie's example:-

$some_array = array(
'first' => 'Hello',
'second' => ' ',
'third' => 'world'
);
echo $some_array['first']; // Outputs Hello


can be done like this too:


$things = array();

$things['define_your_own_keys'] = "My value";
$things['another_name'] = "Another value";
$things['refer_to_this'] = "Something here";

echo print_r($things);//will print the contents of the array

echo $things['refer_to_this'];//outputs "Something here"


As you can see there are many ways of doing the same thing :)

Always good to get clarification :) What reference books are you using out of curiosity, as there are lots out there. One of my preferred reference manuals is one by: Welling & Thompson (google it if you like :))

Though the php.net is a good one ;-p

But for me having a well thumbed book to hand is good.

Have fun with the rest of the project,

Cheers,
MRb

MajorPat

2:33 pm on Apr 7, 2010 (gmt 0)

5+ Year Member



thanks again. I understand the => sign is used in arrays, just wanted to see it in print.

I have a SAMS PHP, APache, MySQL book that I reference, and then bunches of websites. I will check out the book you reference.

On to another note, within my array there are six variables, but this array was built by third party software. Is there a methodology to what the variable would represent as they refer back to a list box on my form?

Matthew1980

2:41 pm on Apr 7, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there MajorPat,

I guess as they would be something like id numbers or something to do with a checkbox or radio. I'm stabbing in the dark there though. Just do a crtl F on the page source to see what they refer to, or even ctrl F on the actual source code. Reverse engineering etc.

Needle & haystack come to mind there too !

Enjoy the project though ;-p

Cheers,
MRb