Closed Thread Icon

Preserved Topic: creating and looping 2-dimensional arrays? (Page 1 of 1) Pages that link to <a href="http://ozoneasylum.com/backlink?for=21033" title="Pages that link to Preserved Topic: creating and looping 2-dimensional arrays? (Page 1 of 1)" rel="nofollow" >Preserved Topic: creating and looping 2-dimensional arrays? <span class="small">(Page 1 of 1)</span>\

 
DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 06-07-2002 22:10

in PHP...
At least that's what I think I have to do.

This is the code:

code:
function getPage($pageName){
Global $connection;
hookUpDb();
if($pageName != ""){
$query = "SELECT page_id,page_type,page_name
FROM pages WHERE page_name = '$pageName'";
} else {
$query = "SELECT page_id,page_type,page_name
FROM pages";
}
$result = mysql_query($query,$connection);
$page = mysql_fetch_array($result);

return $page;
mysql_free_result($result);
mysql_close($connection);
}



I've got this in one include, then I call it from a presentation page like this:

$page = getPage($pageName);

And print what I need from it like this:
echo "<br><br>\n<strong>".$page["page_id"]."</strong><br />\n";
echo "<strong>".$page["page_type"]."</strong><br />\n";
echo "<strong>".$page["page_name"]."</strong><br />\n";

Now, this works fine as long as I pass a valid page name to the function getPage($pageName);.

However, I need to be able to NOT send a page name to the function and get all pages that exist in the database returned, exactly like with the page name.

As it stands today, I only get the first row from the result-set back if I leave the page-name out. (Nothing strange about that)

The SQL is correct, and the getPage() function will loop and print everything IF I do a regular

code:
while ($page = mysql_fetch_row($result)){
print($page[page_id]."<br>");
print($page[page_type]."<br>");
print($page[page_name]."<br>");
}


inside the function, but I don't want that, I want it returned to wherever I call it from so I can work with it there!

So, I'm thinking that I might have to loop it and create something like this:

code:
(pseudo-code below...)


function getPage($pageName){
Global $connection;
hookUpDb();
if($pageName != ""){
$query = "SELECT page_id,page_type,page_name
FROM pages WHERE page_name = '$pageName'";
} else {
$query = "SELECT page_id,page_type,page_name
FROM pages";
}
$result = mysql_query($query,$connection);


while ($page = mysql_fetch_row($result)){

$pages[0] holds array page_1[page_id]]
page_1[page_type]
page_1[page_name]
$pages[1] holds array page_2[page_id]
page_2[page_type]
page_2[page_name]

}

return $pages;
mysql_free_result($result);
mysql_close($connection);
}


This example of course won't work, but am I going in the right direction?
I can't make heads or tails of the examples on multi-dimensional arrays in the php-manual, I've never worked with anything but the simplest arrays before so I'm lost :-(

Then, how would I read from this in order to get what I want from the "inner" array?
I hope this post is understandable...

Thanx/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

[This message has been edited by DmS (edited 06-07-2002). FumbleFingers...]


[This message has been edited by DmS (edited 06-07-2002).]

mr.maX
Maniac (V) Mad Scientist

From: Belgrade, Serbia
Insane since: Sep 2000

posted posted 06-07-2002 22:32

Creating two-dimensional array:

// your code here...
$result = mysql_query($query, $connection);
$pages = array();
while ($page = mysql_fetch_row($result))
{
$pages[] = $page;
}
return $pages;
// your code here...

Looping through two-dimensional array (this is just one how you can do it):

$pages = getPage('');
foreach ($pages as $page)
{
echo "<br><br>\n<strong>".$page["page_id"]."</strong><br />\n";
echo "<strong>".$page["page_type"]."</strong><br />\n";
echo "<strong>".$page["page_name"]."</strong><br />\n";
}

If only one page has been returned, you can use $pages[0]["field_name"] to access array fields.


DmS
Paranoid (IV) Inmate

From: Sthlm, Sweden
Insane since: Oct 2000

posted posted 06-08-2002 22:51

Darn, that simple!

Beautiful, thank you very much mr.Max!

Where would be the best place to learn more about more advanced use of arrays?
(regardless of language, just the principles)
Any tips?
/Dan

{cell 260}
-{ a vibration is a movement that doesn't know which way to go }-

« BackwardsOnwards »

Show Forum Drop Down Menu