Jump to content


Photo

Array Help Please.


  • Please log in to reply
8 replies to this topic

#1 dagnasty

dagnasty
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 16 July 2006 - 05:48 PM

while($row = mysql_fetch_array($result))
{
 $search["{$row['searchquery']}"]++;
 $total["{$row['Count']}"]++;
}

How exactly does this structure the array?

$search[0]; does not print anything, and neither does $search[0][0];

But print_r($search); puts out a result such as this:

Array ( [superman] => 1 [prey] => 1 [pirates] => 1 [stargate] => 1 [lost] => 1 [psp] => 1 [entourage] => 1 [superman returns] => 1 [cars] => 1 [Pirates Of The Caribbean] => 1 )

Can someone please explain how the above code sets up the array? I don't understand it at all.

What should I do to print out a single item in either of the 2 arrays?

#2 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 16 July 2006 - 05:50 PM

Edit:

use this:

while($row = mysql_fetch_array($result))
{
 $search["{$row['searchquery']['prey']}"]++;
 $total["{$row['Count']['pirates']}"]++;
}


#3 dagnasty

dagnasty
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 16 July 2006 - 05:51 PM

$result = @mysql_query("SELECT DISTINCT searchquery, COUNT(*) AS `Count` FROM tscrapestats GROUP BY searchquery ORDER BY `Count` DESC LIMIT 10");

#4 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 16 July 2006 - 05:52 PM

check the update ^^ hope it helps define the problem.

#5 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 July 2006 - 05:55 PM

$search[0]; does not print anything, and neither does $search[0][0];

That is because you have no numerical keys for that array. $search['pirates'] holds the value "1". The "++" on the end of your variables (or array keys) is just incrementing it's value by one.

#6 dagnasty

dagnasty
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 16 July 2006 - 06:06 PM

so other than print_r there's no way to print each individual item? I'm so completely lost with arrays...


Can I use mysql_result to print the Count and the searchquery?

#7 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 July 2006 - 06:07 PM

<?php
$result = @mysql_query("SELECT searchquery, COUNT(*) AS `Count` FROM tscrapestats GROUP BY searchquery ORDER BY `Count` DESC LIMIT 10");

while($row = mysql_fetch_array($result))
{
 $search[$row['searchquery']]++;
 $total[$row['Count']]++;
}
?>
As you are grouping by searchquery, DISTINCT is unnecessary. They will be anyway.

The keys to the $search array are 'superman', 'lost' etc.

eg. These will always be 1 as each occurs once in the results
echo $search['superman'];

The keys to the $total array are the count values from the query
eg how many had a count of 10?

echo $total[10];

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 July 2006 - 06:08 PM

try
<?php
$result = @mysql_query("SELECT searchquery, COUNT(*) AS `Count` 
           FROM tscrapestats 
           GROUP BY searchquery 
           ORDER BY `Count` DESC 
           LIMIT 10");

while(list($sq, $c) = mysql_fetch_row($result))
{
 echo "$sq : $c <br>";
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 dagnasty

dagnasty
  • Members
  • PipPipPip
  • Advanced Member
  • 66 posts

Posted 16 July 2006 - 06:17 PM

That works great and is much easier to understand than what I was doing before.

Thanks guys.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users