calling a php function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • thecoolone

    #1

    calling a php function

    I am trying to call a function from "a href" inside the same page.
    this is the code:
    echo "<br><br><a href=\"isearch( $query)\">More results from
    Mysite</a>";
    // calling the function isearch

    function isearch($query)

    {$query=urlenco de($query);

    $request='http://api.search.yaho o.com/WebSearchServic e/V1/webSearch?appid =Ja
    hangir&query=' .urlencode($que ry).
    '&output=php&re sults=100&site= mysite.com;

    $response=file_ get_contents($r equest);

    if ($response === false) {

    die('Request failed');}

    $phpobj=unseria lize($response) ;

    $count=$phpobj["ResultSet"]["totalResultsRe turned"];

    if($phpobj["ResultSet"]["totalResultsAv ailable"]==0)

    {echo "<br>NO RESULTS TO DISPLAY"; }

    echo "<tr>"; echo "<h4 style=\"color:# FF0000\" align=\"center\ ">Results
    from
    Mysite</h4>";

    for($i=0;$i<$co unt;$i++)

    { echo '<pre>';

    $no=$i+1;

    $url=$phpobj["ResultSet"]["Result"]["$i"]["Url"];

    echo "<h3>" ."$no. ". "<a href=\"$url\">" .
    $phpobj["ResultSet"]["Result"]["$i"]["Title"] . "</a></h3>"; echo
    "</tr>";

    echo "<tr>"; echo $phpobj["ResultSet"]["Result"]["$i"]["Summary"]; echo
    "</tr>";

    echo "<br>"; echo "<tr>"; echo "<b>"
    ..$phpobj["ResultSet"]["Result"]["$i"]["Url"] ."</b>"; echo "</tr>";

    echo "<br>"; $link=$phpobj["ResultSet"]["Result"]["$i"]["DisplayUrl "];

    echo "<tr>"; echo "<a href=\"$url\">$ link</a>"; echo "</tr>";

    echo '</pre>'; }}



    whenever i try to execute this function i either get an "Object not
    found
    error" or "Access Forbidden error".

    Can someone tell me where am i going wrong here??

    thanks in advance

  • no@email.com

    #2
    Re: calling a php function

    thecoolone wrote:
    I am trying to call a function from "a href" inside the same page.
    this is the code:
    echo "<br><br><a href=\"isearch( $query)\">More results from

    you must break a string to call a function.
    try this:

    echo "<br><br><a href=\"" . isearch($query) . "\">More results from
    Mysite</a>";

    Comment

    • no@email.com

      #3
      Re: calling a php function

      oh, yeah
      and first write your function

      function isearch() {
      ....

      }

      and then call the

      echo "<br><br><a href=\"" . isearch($query) . "\">More results from
      Mysite</a>";

      Comment

      • BKDotCom

        #4
        Re: calling a php function


        thecoolone wrote:
        I am trying to call a function from "a href" inside the same page.
        this is the code:
        echo "<br><br><a href=\"isearch( $query)\">More results from
        It's not clear what you're wanting to do.
        a) Do you want a link that executes/calls isearch?
        b) Or, do you want the href link/value to contain the results of
        isearch?

        I'm thinking you want (a). If so, try something like this:
        echo '<br><br><a
        href="?action=s earch&amp;query ='.urlencode($q uery).'">More
        results</A>';

        then you'd need to add some code to your page likey so:

        if ( isset($_GET['action']) && $_GET['action'] == 'search' )
        {
        if ( !empty($_GET['query']) )
        isearch($_GET['query']);
        }

        Comment

        Working...