Jump to content

Archived

This topic is now archived and is closed to further replies.

dagnasty

Array Help Please.

Recommended Posts

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

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?

Share this post


Link to post
Share on other sites
[b]Edit:[/b]

use this:

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

Share this post


Link to post
Share on other sites
$result = @mysql_query("SELECT DISTINCT searchquery, COUNT(*) AS `Count` FROM tscrapestats GROUP BY searchquery ORDER BY `Count` DESC LIMIT 10");

Share this post


Link to post
Share on other sites
check the update ^^ hope it helps define the problem.

Share this post


Link to post
Share on other sites
[quote]$search[0]; does not print anything, and neither does $search[0][0];[/quote]
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[code]<?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']]++;
}
?>[/code]
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
[code]echo $search['superman'];[/code]

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

[code]echo $total[10];[/code]

Share this post


Link to post
Share on other sites
try
[code]<?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>";
}
?>[/code]

Share this post


Link to post
Share on other sites
That works great and is much easier to understand than what I was doing before.

Thanks guys.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.