How to insert an array inside an array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

How to insert an array inside an array?

Post by ghank »

I guess this is called a multidimensional array? I got it to work but it slowed my page down adding the $hlinksrow array. I assume because it is having to run 40 mysql queries. I am trying to have it just run 1 query and pull 40 entries from the database but I can't get it to work. How do you put an array inside an array? By the way, this code is making links for my page and randomizing the anchor text.

Code: Select all

<?php 

$id = $row["id"];
$stateabb = $row["stateabb"];
$links = mysql_query("SELECT * FROM table1 WHERE id > '$id' AND stateabb = '$stateabb' LIMIT 40"); 

if ($linksrow = mysql_fetch_array($links)) {

	      do {

$hlinks = mysql_query("SELECT hlink FROM table2 ORDER BY RAND() LIMIT 1");

?>

<a style="text-decoration: none" href="/dir/subdir/<?php
 
echo $linksrow["url"];

?>">

<?php

echo $linksrow["city"]," ";


$hlinksrow = mysql_fetch_array($hlinks);

              echo $hlinksrow["hlink"]," - ";

} while ($linksrow = mysql_fetch_array($links));

	    } else {

	      echo "None Available";	

	    }

  ?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

how about:
untested

Code: Select all

<?php 

$id = $row["id"]; 
$stateabb = $row["stateabb"]; 
$links = mysql_query("SELECT * FROM table1 WHERE id > '$id' AND stateabb = '$stateabb' LIMIT 40");
$linkdata = array();
while($row = mysql_fetch_assoc($links))
{
  $linkdata[] = $row;
}

if(sizeof($linkdata) == 0)
  die("None Available");

$hlinks = mysql_query("SELECT hlink FROM table2 ORDER BY RAND() LIMIT 40");
$num = mysql_num_rows($hlinks);

if($num == 0)
  die("None Available");

$hlinkdata = array();

while($row = mysql_fetch_assoc($hlinks))
  $hlinkdata[] = $row;

$count = 0;
foreach($linkdata as $link)
{
  echo "\n<a style="text-decoration: none" href="/dir/subdir/{$link['url']}">\n";
  echo $linksrow["city"]." "; 
  echo $hlinkdata[$count % $num]["hlink"]." - "; 
  $count++;
}

?>
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Post by ghank »

Thanks, that seems simpler. Just had to change

echo $linksrow["city"]." ";

to

echo $links["city"]," ";


Now I just need to figure out how it does it (I'm a noobie).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

with the comma, it won't print that space...
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

with the comma, it won't print that space
Sure it will ;) You can concat with a comma too :o
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that just sounds weird...
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

seems like an echo's magic:

Code: Select all

weirdan@office:~$ php -r 'echo "something", "something_else\n";'
somethingsomething_else
weirdan@office:~$ php -r '$q = ("something", "something_else\n"); echo $q;'
PHP Parse error:  parse error in Command line code on line 1

Parse error: parse error in Command line code on line 1
[update]
That is, here is excerpt from the manual:
PHP Manual wrote: echo() is not actually a function (it is a language construct) so you are not required to use parentheses with it. In fact, if you want to pass more than one parameter to echo, you must not enclose the parameters within parentheses.
[/update]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd still suggest using the . operator as it'll work with every other function and whatnot without issue.
Post Reply