Jump to content

Array Help Please.


dagnasty

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?
Link to comment
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.
Link to comment
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]
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.