Forum Moderators: coopster

Message Too Old, No Replies

Multidimensional Arrays

Retrieve sql date into a Multidimensional Arrays

         

TheLazyAce

4:08 am on Jan 18, 2011 (gmt 0)

10+ Year Member



Being new with the PHP, but having other programming knowledge I would like to create clean code to create this multidimensional arrays.

I have 11 records I have pulled from a DB with 100 records. In the loop as I read the result I would like to create this basic structure:

Board Member
- 1sdt Name & Last name to create Full Name
- Board Position
- email
- Active

Then loop back to the new row.

I know I can create a lot of variables, in a dirty way, and I can also hack at this code for days until I figure it out. Knowing that there are more experienced PHP coders out there, I know someone can show me the light.

Thanks for any input.

Ed

Readie

12:54 pm on Jan 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this?


$sql = '
SELECT
CONCAT_WS(' ', first_name, last_name) AS full_name,
board_position,
email,
active
FROM
board_members
';
$result = mysql_query($sql);
$members = array();
while($res = mysql_fetch_assoc($result)) {
$members[] = $res;
}

echo '<pre>' . print_r($members, true) . '</pre>';

TheLazyAce

1:14 pm on Jan 18, 2011 (gmt 0)

10+ Year Member



Thank you. I missed what I was doing by 2 miles. I will study this to understand the way it works.

JAB Creations

4:20 pm on Jan 18, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just for anyone referencing this thread a quick way to look at an array and how to echo (and thus access) parts of an array...

- John

<?php
echo '<div><pre>';
print_r($array);
echo '</pre></div>';

echo $array['array1']['array2']['array3'];
?>

TheLazyAce

7:37 pm on Jan 18, 2011 (gmt 0)

10+ Year Member



Something like this?


$sql = 'SELECT CONCAT_WS(' ', first_name, last_name) AS full_name, board_position, email, activeFROM board_members';$result = mysql_query($sql);$members = array();while($res = mysql_fetch_assoc($result)) { $members[] = $res;}echo '<pre>' . print_r($members, true) . '</pre>';


I am not sure here. Where is the multidimension array? lets say I have this data:

$Result = Row 0 1st name, last name, address, email, Position, active
........ row 11 ......

I create a loop to read the row.

Memebers array = 1st & Last name
email array = email
office array = position
active array = 0 or 1

what is the clean way of creating this code?


My thoughts was

$first (array) = 1st name;
$last (array) = last name;
$FullName (array) = $first(array) . " " . $last(array);
.......

Then when I am finished extracting all the rows I can determine
1. If the active = true
html code fullname position With fullname being the link to do a mailto:

Did I just muddy the water more becasue of my lack of knowledge?

Ed

rocknbil

6:38 pm on Jan 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Let's back up a step. Exactly what is the end result?

I ask because there may not be a need to use up memory by storing the data in arrays. Scalar variables, or outputing to the browser as you go, are both fairly lightweight but if you have to hold a large chunk in memory for an array, it is demanding on resources.

So if you loop through to output to the browser, store in a scalar,

$output = null;
while ($row = mysql_fetch_array($result)) {
$output .= "1sdt Name Last name Board Position email Active ";
}
if ($output "echo $output"; }
else { echo "no results"; }

The if/else demonstrates the advantage of using a scalar. Otherwise,

while ($row = mysql_fetch_array($result)) {
echo "1sdt Name Last name Board Position email Active ";
}

So what if it's not browser output? Same concept.

while ($row = mysql_fetch_array($result)) {
mail(email, subject, message,headers);
}

So I guess the question is what are you doing with this array? It may not be necessary.

}

TheLazyAce

7:39 pm on Jan 19, 2011 (gmt 0)

10+ Year Member



Let's back up a step. Exactly what is the end result?


The end result is output in a browser that would look like this:

CSS page <start>

..... info.....

Your Board

1. john smith Pres
2. Jack Jones VP
3....... 11.

The member of the org will be able to click on the officers name which would bring up the views email client to allow that person to send email to that one person.

The board at the top of this box will have a Mailto link also, except the mailto: would be mailto all eleven members of the board.

The only reason I need the email field is for the mailto link. I need the active field because the board member may have left the org in which case the board leaves that spot open until the next election which could be up 11 months away.

Thanks

Ed

rocknbil

2:28 am on Jan 20, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, see previous about storing in a scalar or just put that PHP bit at that location in the page and echo it out. (?)