Jump to content

help with the structuring and placing the http_build_query() function.


Recommended Posts

<?php if ($currentpage > 1) {
   // show << link to go back to page 1 
http_build_query( $strName,$strZipCode,$strState,$arrFoodTypes,$arrOfferings)
  echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&strZipCode=".$strZipCode."><<</a> ";
   // get previous page num
   $prevpage = $currentpage - 1;
   // show < link to go back to 1 page 
http_build_query( $strName,$strZipCode, $strState, $arrFoodTypes, $arrOfferings)
   echo  " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage&strZipCode=".$strZipCode."'><</a> ";
} // end if ?>

 

The above script is part of a pagination script and I am trying to use the http_build_query function to populates the variables inside () the parenthesis each time a users click in one of the pages of the pagination and to display a new set up of rows found in each page according to the variables values. Right now strZipCode would work ok but the other variables inside the parenthesis are array, plus the http_build_query should be implemented inside the <a hrft= "link"  tags but I don't have any idea on how to formulate it.

 

Tosted brain here, need to be refreshed, I have put the variable &strZipCode=".$strZipCode." in the url which comes from the POST variable extraction from the top of the script as below:

 

<?php // Extract POST variables and escape
    $strName = isset($_POST['frmSearch']['name'])?/*mysql_real_escape_string(*/$_POST['frmSearch']['name']/*)*/:'';
    $strZipCode = isset($_POST['frmSearch']['zipcode'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['zipcode']/*)*/:'';
    $strState = isset($_POST['frmSearch']['state'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['state']/*)*/:'';
    $arrFoodTypes = isset($_POST['frmSearch']['food_types'])?$_POST['frmSearch']['food_types']:array();
    $arrOfferings = isset($_POST['frmSearch']['offerings'])?$_POST['frmSearch']['offerings']:array();
?>

 

The script above will extract all the variables comiing from a form in index.php to indexpagiination.php

 

I have been suggested and read to use the http_build_query for this purpose.

you are doing it wrong, http_build_query accepts an array (can be multidimensional). you first need to build that array and then pass it to the function. right now you are passing it multiple values. check out this link http://php.net/manual/en/function.http-build-query.php

The array is build already and it is in within thi variable

 

http_build_query($arrRestaurants)

 

But the output is (array).

 

Do I have to serialize it in order to have the values separately?

 

 

 

no it should do the processing for you. if you were to do this:

 

$test = array('test'=>'yeah','testing'=>'no');
$query = http_build_query($test);
echo $query;

 

it would output test=yeah&testing=no. if you do not create an associative array you could do this:

 

$test = array('yes','no');
$query = http_build_query($test,'test_');
echo $query;

 

it would ouput test_0=yes&test_1=no where it just uses the second parameter as the variable

I guess that if I build an array should be dynamically.. because the variables are coming from a form in a nother page, the thing is that it doesn't refresh once clicked in one of the number of the pagination links, the variables doesn't exist once the pagination get going.

 

<?php echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1&". http_build_query array( 'frmSearch'=>'name','frmSearch'=>'state','frmSearch'=>'zip','frmSearch'=>'name','frmSearch'=>'food_types','frmSearch'=>'offerings'). "'><<</a>><<</a> ";?>

 

Above are all the parameters coming from the form... that will be pass again after an user click in the second page of the pagination in indexpagination.php

 

 

Hope that would work..

 

I was wondering how will I place the variable $query when you first posted here Because I knew that opening a new query was not an option, but now I see how it can be done. I tend to see one possibility always.

 

i will test this.

it's throwing this error

 

Notice: Undefined index: name in C:\wamp\www\nyhungry\indexpagination.php on line 286

 

Notice: Undefined index: state in C:\wamp\www\nyhungry\indexpagination.php on line 286

 

Notice: Undefined index: zipcode in C:\wamp\www\nyhungry\indexpagination.php on line 286

 

Notice: Undefined index: food_types in C:\wamp\www\nyhungry\indexpagination.php on line 286

 

Notice: Undefined index: offerings in C:\wamp\www\nyhungry\indexpagination.php on line 286

 

 

This is the set up which is similar,

 

<?php $arr= array( 'name'=>$_POST['name'],'state'=>$_POST['state'], 'zip'=>$_POST['zipcode'], 'food_types'=>$_POST['food_types'],'offerings'=>$_POST['offerings']);
$query3 = http_build_query($arr);?>

 

and the extract of the variable are on the very top of the document...

 

<?php 
<?php // Extract POST variables and escape
    $strName = isset($_POST['frmSearch']['name'])?/*mysql_real_escape_string(*/$_POST['frmSearch']['name']/*)*/:'';
    $strZipCode = isset($_POST['frmSearch']['zipcode'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['zipcode']/*)*/:'';
    $strState = isset($_POST['frmSearch']['state'])/*mysql_real_escape_string(*/?$_POST['frmSearch']['state']/*)*/:'';
    $arrFoodTypes = isset($_POST['frmSearch']['food_types'])?$_POST['frmSearch']['food_types']:array();
    $arrOfferings = isset($_POST['frmSearch']['offerings'])?$_POST['frmSearch']['offerings']:array();
?>

 

Wonder where those undefined index errors coming from...

 

 

may be the set up of the http_build_query function is not place insed of the scope..?

are you making actually making a post or are you doing a get?

 

edit: nvm i see the problem. you should be using those elements instead if they are at the top of the document:

 

<?php $arr= array( 'name'=>$strName);
$query3 = http_build_query($arr);?>

 

alternatively you could use:

 

<?php $arr= array( 'name'=>$_POST['frmSearch']['name']);
$query3 = http_build_query($arr);?>

 

You were trying to access it as $_POST['name'], and it should be $_POST['frmSearch']['name'] by looking at your other code.

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.