co.ador Posted December 12, 2009 Share Posted December 12, 2009 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/ Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976105 Share on other sites More sharing options...
co.ador Posted December 12, 2009 Author Share Posted December 12, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976113 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976118 Share on other sites More sharing options...
co.ador Posted December 12, 2009 Author Share Posted December 12, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976129 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 you would be better to build the array first: $get = array('name' => $_GET['name'], 'age' => $_GET['age']); $query = http_build_query($get); echo '<a href="somepage.php?$query">Some Page</a>'; Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976133 Share on other sites More sharing options...
co.ador Posted December 12, 2009 Author Share Posted December 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976135 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 let me know if you have anymore questions. Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976137 Share on other sites More sharing options...
co.ador Posted December 12, 2009 Author Share Posted December 12, 2009 Ok I will thanks. Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976141 Share on other sites More sharing options...
co.ador Posted December 12, 2009 Author Share Posted December 12, 2009 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..? Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976156 Share on other sites More sharing options...
ngreenwood6 Posted December 12, 2009 Share Posted December 12, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/184904-help-with-the-structuring-and-placing-the-http_build_query-function/#findComment-976157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.