Page 1 of 1

How to insert an array inside an array?

Posted: Wed Jun 23, 2004 11:49 am
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";	

	    }

  ?>

Posted: Wed Jun 23, 2004 12:07 pm
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++;
}

?>

Posted: Wed Jun 23, 2004 12:46 pm
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).

Posted: Wed Jun 23, 2004 12:47 pm
by feyd
with the comma, it won't print that space...

Posted: Wed Jun 23, 2004 12:49 pm
by markl999
with the comma, it won't print that space
Sure it will ;) You can concat with a comma too :o

Posted: Wed Jun 23, 2004 12:50 pm
by feyd
that just sounds weird...

Posted: Wed Jun 23, 2004 1:11 pm
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]

Posted: Wed Jun 23, 2004 1:15 pm
by feyd
I'd still suggest using the . operator as it'll work with every other function and whatnot without issue.