Current location: Hot Scripts Forums » Programming Languages » PHP » This strange error, anyone had this ?


This strange error, anyone had this ?

Reply
  #11 (permalink)  
Old 05-19-06, 08:54 PM
klaniak klaniak is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Underground
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
okay so here is the function:

PHP Code:

function GoogleSearch($keyword$file$delay)

{
$res=0;
$fp=fopen('./files/'.$file'a+');
for(
$a=50;$a>0;$a--)
{
sleep($delay); //waiting time until next results
$params = array(
'key' => 'I can t put here my google Key',
'q' => $keyword,
'start' => $res,
'maxResults' => 10,
'filter' => false,
'restrict' => '',
'safeSearch' => false,
'lr' => 'lang_en',
'ie' => '',
'oe' => ''
);

$soapclient = new soapclient("http://api.google.com/search/beta2");
$searchresults $soapclient->call("doGoogleSearch"$params,"urn:GoogleSearch");
//print_r($searchresults);
// echo $uri=$searchresults[resultElements][$i][URL].'<br>';
$ggl=$searchresults[resultElements];

for(
$i=0;$i<=9;$i++)
{
$resul=$ggl[$i][URL];
echo 
$resul.'<br>';
$fpw=fwrite($fp$resul."\n");
flush(); //results real time
}
//echo '<br><br>';
$res+=10;
}
$fpc=fclose($fp);

Now this worked like a charm when I did the results backwords.
But When I modified the value of $a, for example I put there 10;
I saw some results let's say like 5 I realy don't remember and then bam the error:
Fatal error: Cannot use string offset as an array
the line indicating the error was this one:
$resul=$ggl[$i][URL];
Once again I telled to myself, "what the hell is going on ?"
so you see the code builded in a different way

Last edited by klaniak; 05-19-06 at 09:03 PM.
Reply With Quote
  #12 (permalink)  
Old 05-19-06, 09:22 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
My guess is that the $i variable exceeds the number of array elements. The times it works, there are enough elements. The times it has an error, there are not enough. You would hope that there would have been an "index out of range" message, but the message you are getting is related to the index value/type...
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #13 (permalink)  
Old 05-19-06, 11:09 PM
klaniak klaniak is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Underground
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by mab
My guess is that the $i variable exceeds the number of array elements. The times it works, there are enough elements. The times it has an error, there are not enough. You would hope that there would have been an "index out of range" message, but the message you are getting is related to the index value/type...
You say that but every result has 10 links maxim
so from 0 to 9 it's okay there is no exeeding and if it's not there then the output is empty or better said no result, what do you expect me to believe ?

I think I need to rewrite the code... If somebody has any Ideea how to remake this code please post, also try this function on your server and tell me what happens.

10x
Reply With Quote
  #14 (permalink)  
Old 05-19-06, 11:52 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Even though you request a maximum of 10 results, there is no guarantee you will get 10 each and every time.

I recommend replacing the For loop with a foreach loop -
PHP Code:

foreach($searchresults['resultElements'] as $result) {


// code here to output the results


This will only process as many elements as there are.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #15 (permalink)  
Old 05-20-06, 08:28 AM
klaniak klaniak is offline
Wannabe Coder
 
Join Date: Apr 2005
Location: Underground
Posts: 119
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by mab
Even though you request a maximum of 10 results, there is no guarantee you will get 10 each and every time.

I recommend replacing the For loop with a foreach loop -
PHP Code:

foreach($searchresults['resultElements'] as $result) {


// code here to output the results


This will only process as many elements as there are.
I put the script to a online server. It works with no problems
any key that I insert works like a charm. You should test it yourself and see.
I hope not to see the error again...

Also do you know how to install simplexml ? Because I want to install it on my online server
On my localhost I have php 5.1.4 and apache 2.2.2 and simplexml is installed by default. On my server is not installed
Reply With Quote
  #16 (permalink)  
Old 05-20-06, 09:20 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Also do you know how to install simplexml ? Because I want to install it on my online server
On my localhost I have php 5.1.4 and apache 2.2.2 and simplexml is installed by default. On my server is not installed
This is PHP5 only - http://www.php.net/manual/en/ref.simplexml.php
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Javascript Error benholmes12345 JavaScript 2 03-03-06 03:48 AM
Strange Cookie Problems darkreaper PHP 1 12-30-04 06:39 PM
Strange script behavior attached kjmatthews PHP 2 07-13-04 11:00 AM
very strange functions issue jolac PHP 3 05-13-04 01:54 AM
This is a strange index.php problem! Balaheler PHP 2 10-16-03 10:11 PM


All times are GMT -5. The time now is 07:13 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.