You are here:  » saving resources


saving resources

Submitted by Demarco on Mon, 2009-02-23 12:07 in

Hi Dave, I'm just purchased your script.. When i see that u really provide great support, i dont think twice to purchase your script.. :)

Btw, u already know what will i do with your script, because i have sent u an email before.

now i have acouple question:

1. how to get the total number of item without using "myRecordCounter"?, but i want to directly parse it (from count="x") in the xml file. because i think this is faster than using "myRecordCounter".

if i'm using string:// will all the data be deleted after the script finished and won't be used for future request right?

btw, can u check my script wheter it is good enough or something need to be changed to save resources. i'm totally new with php..

<?php
  require("MagicParser.php");
  $url = "{link saved}";
  $xml = "";
  $fp = fopen($url,"r");
  while(!feof($fp))
  {
    $xml .= fread($fp,1024);
  }
  fclose($fp);
  $total_number_of_items = 0;
  function myRecordCounter($record)
  {
    global $total_number_of_items;
    $total_number_of_items++;
  }
  MagicParser_parse("string://".$xml,"myRecordCounter","xml|XML/RESULT/RECORD/");
  if ($total_number_of_items > 0) echo '<h2>'._sponsored.':</h2>';
  function myRecordHandler($record)
  {
    $click_data2 = base64_encode($record["CLICK"]);
    print "<table width=100%><tr><td>";
    print "<a target='_blank' href='recommend.php?u=".$click_data2."'>".$record["TITLE"]."</a><br>";
    print "<small>".$record["DESCRIPTION"]."</small><br>";
    print "<font class=url><small>".$record["URL"]."</small></font><br></td></tr></table>";
  }
  MagicParser_parse("string://".$xml,"myRecordHandler","xml|XML/RESULT/RECORD/");
  if ($total_number_of_items > 0) echo "<b>Related Searches:</b><br>";
  function myRecordRelated($record)
  {
    $kwmin=str_replace(' ', '-', $record["KEYWORD"]);
    print "<a href='/search/".$kwmin."'>".$record["KEYWORD"]."</a><br>";
  }
  MagicParser_parse("string://".$xml,"myRecordRelated","xml|XML/RELATED/KEYWORD/");
?>

Thanks..

P.S. Please hide my url

Submitted by support on Mon, 2009-02-23 12:33

Hello Demarco,

Your script looks very good!

I think it will be very slightly quicker to get count=x from the XML rather than having multiple calls to myCounterHander. To do this, you can replace this code:

  $total_number_of_items = 0;
  function myRecordCounter($record)
  {
    global $total_number_of_items;
    $total_number_of_items++;
  }
  MagicParser_parse("string://".$xml,"myRecordCounter","xml|XML/RESULT/RECORD/");

...with this version:

  $total_number_of_items = 0;
  function myResultHandler($result)
  {
    global $total_number_of_items;
    $total_number_of_items = $result["RESULT-COUNT"];
  }
  MagicParser_parse("string://".$xml,"myRecordCounter","xml|XML/RESULT/");

However, to do this, Magic Parser is still reading the whole XML file. If you don't need to know the total number, but only need to know that there is at least one, this would be the very quickest way:

  $total_number_of_items = 0;
  function myRecordCounter($record)
  {
    global $total_number_of_items;
    $total_number_of_items++;
    return TRUE;
  }
  MagicParser_parse("string://".$xml,"myRecordCounter","xml|XML/RESULT/RECORD/");

In other words, just adding return TRUE; to myRecordCounter will make it stop after the first record, so $total_number_of_items will be 1. If you do need to know the actual number, use the version above.

About string:// you are correct, it will only "live" for that script, and next time the XML will be fetched again.

Hope this helps!
Cheers,
David.

Submitted by Demarco on Mon, 2009-02-23 12:54

Thnaks for your fast response Dave,

btw, why $total_number_of_items always shows 0 even theres a result..

can u fix it?.

Thank You

  <?php
    require("MagicParser.php");
  $url = "{link saved}";
  $xml = "";
  $fp = fopen($url,"r");
  while(!feof($fp))
  {
    $xml .= fread($fp,1024);
  }
  fclose($fp);
  $total_number_of_items = 0;
  function myRecordCounter($record)
  {
    global $total_number_of_items;
    $total_number_of_items++;
    return TRUE;
  }
  MagicParser_parse("string://".$xml,"myRecordCounter","xml|XML/RESULT/RECORD/");
  print $total_number_of_items;
  if ($total_number_of_items = 0) echo '<h2>'._sponsored.':</h2>';
  function myRecordHandler($record)
  {
  $click_data2 = base64_encode($record["CLICK"]);
  print "<table width=100%><tr><td>";
  print "<a target='_blank' href='recommend.php?u=".$click_data2."'>".$record["TITLE"]."</a><br>";
  print "<small>".$record["DESCRIPTION"]."</small><br>";
  print "<font class=url><small>".$record["URL"]."</small></font><br></td></tr></table>";
  }
    MagicParser_parse("string://".$xml,"myRecordHandler","xml|XML/RESULT/RECORD/");
  if ($total_number_of_items > 0) echo "<b>Related Searches:</b><br>";
  function myRecordRelated($record)
  {
  $kwmin=str_replace(' ', '-', $record["KEYWORD"]);
  print "<a href='/search/".$kwmin."'>".$record["KEYWORD"]."</a><br>";
  }
    MagicParser_parse("string://".$xml,"myRecordRelated","xml|XML/RELATED/KEYWORD/");
?>

Submitted by support on Mon, 2009-02-23 12:56

Hello Demarco,

That is a mistake on this line:

if ($total_number_of_items = 0) echo '<h2>'._sponsored.':</h2>';

For comparison, it must be == not = (which is assignment), so you need:

if ($total_number_of_items == 0) echo '<h2>'._sponsored.':</h2>';

Hope this helps!
Cheers,
David.

Submitted by Demarco on Mon, 2009-02-23 13:22

Hi David,

Thanks it works now..

btw, please delete my link above..

Thank You..

Submitted by Demarco on Mon, 2009-02-23 17:08

Dave, please help me i'm now got another problem..

when i'm try to implement it to another script it shows:

Parse error: syntax error, unexpected T_REQUIRE in /home/xxx/public_html/includes/parser.php(319) : eval()'d code on line 2

here's the parser.php code:

{code saved}

Submitted by support on Mon, 2009-02-23 17:28

Hi Demarco,

If you haven't been editing includes/parser.php as part of what you have done so far; it's most likely being caused by something elsewhere, before this file is included. Did you try to add require("MagicParser.php"); and if so - check the syntax around where you added that code and make sure it is valid to use require() at that point - you may need to use it higher up in your script.

It should not be used within a function in this instance (because MagicParser.php itself contains functions)...

Hope this helps,
Cheers,
David.

Submitted by Demarco on Mon, 2009-02-23 18:23

now i can get it working to parse the ads and showing it.. i'm haven't change the structure, just like before..

all the script seems working fine only the $total_number_of_items that have problem,

When i print the $total_number_of_items, it always shows 0, i think its reset somewhere in the script before it get printed..

i'm using $total_number_of_items to set wheter to show a text or not.

if ($total_number_of_items > 0) echo "Related Searches:";

can u suggest any way around it so it can show a text if there's a results?

Thanks

Submitted by support on Mon, 2009-02-23 18:25

Hello Demarco,

Do you mean to show something if there are no results? At the moment, you are looking at $total_number_of_items, what you would normally do with this is:

if ($total_number_of_items)
{
  // add your parse code here
}
else
{
  echo "There are no extra results!";
}

Hope this helps!
Cheers,
David.

Submitted by Demarco on Mon, 2009-02-23 18:46

Yes, I mean is there a method to accomplish that without using $total_number_of_items? because $total_number_of_items keeps getting reset to 0 after get printed, but the other xml data get parsed and showed correctly without problem..

Submitted by support on Mon, 2009-02-23 18:48

Hi Demarco,

That would imply that there is still $total_number_of_items = 0 in the code.

Would you like to email me your latest version and I'll have a look for you...

Cheers,
David.

Submitted by Demarco on Mon, 2009-02-23 18:54

Sure, what script do you mean?

magicparser or my other script that i want to integrate?

Thanks

Submitted by support on Mon, 2009-02-23 18:57

Hi Demarco,

Whichever script (PHP file) contains your myRecordHandler functions and other code...

Cheers,
David.

Submitted by Demarco on Mon, 2009-02-23 19:54

Hi David,

I've sent you the script to your email..

Thanks for take a look..

Submitted by Demarco on Tue, 2009-02-24 01:00

Hello David.

Please check your email again..

Thank You..

Submitted by Demarco on Tue, 2009-02-24 17:07

Hey David why when i added new variable to sponsoredresults.tmpl (attached to your email) and
have declared global for all of it, its shows previous error..

What do you changed to make it work?..

do you only make the variable global or change any other things?

Thanks

Submitted by support on Tue, 2009-02-24 17:57

Hello Demarco,

I just replied to your email - I think it is just because your script had # as a comment which is not supported by PHP. In PHP, you can use:

// single or end of line comment or

/*
Block Comment
*/

Cheers!
David.