PDA

View Full Version : Error when doing SEARCH page



thientanchuong
12-31-2009, 01:43 PM
I create more 3 files called config1.php, common1.php and database1.php

test my page

http://i.imagehost.org/0127/search_code5.jpg

The error message said that I do not need the sesstion_start() in both database1.php and common1.php, so I remmove the function. Finally, there is no error message when I test :

http://f.imagehost.org/0178/search_fix1.jpg

But error paging is still appearing, when I click on page 2 for example, the page turns out nothing again:

http://i.imagehost.org/0725/search_error1.jpg

I noticed that the address bar which is


http://yoongda.com/search.php?page=2&

is missing search value, so address bar shoud show the link:


http://yoongda.com/search.php?page=2&search=a

How can I add the search value after http://yoongda.com/search.php?page=2& such as search=a ?

this is my common1.php which contains paging function.


<?php
/*
Contain the common functions
required in shop and admin pages
*/
require_once 'config1.php';
require_once 'database1.php';

/*
Make sure each key name in $requiredField exist
in $_POST and the value is not empty
*/
function checkRequiredPost($requiredField) {
$numRequired = count($requiredField);
$keys = array_keys($_POST);

$allFieldExist = true;
for ($i = 0; $i < $numRequired && $allFieldExist; $i++) {
if (!in_array($requiredField[$i], $keys) || $_POST[$requiredField[$i]] == '') {
$allFieldExist = false;
}
}

return $allFieldExist;
}

function getShopConfig()
{
// get current configuration
$sql = "SELECT sc_name, sc_address, sc_phone, sc_email, sc_shipping_cost, sc_order_email, cy_symbol
FROM tbl_shop_config sc, tbl_currency cy
WHERE sc_currency = cy_id";
$result = dbQuery($sql);
$row = dbFetchAssoc($result);

if ($row) {
extract($row);

$shopConfig = array('name' => $sc_name,
'address' => $sc_address,
'phone' => $sc_phone,
'email' => $sc_email,
'sendOrderEmail' => $sc_order_email,
'shippingCost' => $sc_shipping_cost,
'currency' => $cy_symbol);
} else {
$shopConfig = array('name' => '',
'address' => '',
'phone' => '',
'email' => '',
'sendOrderEmail' => '',
'shippingCost' => '',
'currency' => '');
}

return $shopConfig;
}

function displayAmount($amount)
{
global $shopConfig;
return $shopConfig['currency'] . number_format($amount);
}

/*
Join up the key value pairs in $_GET
into a single query string
*/
function queryString()
{
$qString = array();

foreach($_GET as $key => $value) {
if (trim($value) != '') {
$qString[] = $key. '=' . trim($value);
} else {
$qString[] = $key;
}
}

$qString = implode('&', $qString);

return $qString;
}

/*
Put an error message on session
*/
function setError($errorMessage)
{
if (!isset($_SESSION['plaincart_error'])) {
$_SESSION['plaincart_error'] = array();
}

$_SESSION['plaincart_error'][] = $errorMessage;

}
function displayError()
{
if (isset($_SESSION['plaincart_error']) && count($_SESSION['plaincart_error'])) {
$numError = count($_SESSION['plaincart_error']);

echo '<table id="errorMessage" width="550" align="center" cellpadding="20" cellspacing="0"><tr><td>';
for ($i = 0; $i < $numError; $i++) {
echo ' ' . $_SESSION['plaincart_error'][$i] . "<br>\r\n";
}
echo '</td></tr></table>';

// remove all error messages from session
$_SESSION['plaincart_error'] = array();
}
}
function getPagingQuery($sql, $itemPerPage = 10)
{
if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$page = (int)$_GET['page'];
} else {
$page = 1;
}

// start fetching from this row number
$offset = ($page - 1) * $itemPerPage;

return $sql . " LIMIT $offset, $itemPerPage";
}
function getPagingLink($sql, $itemPerPage = 10, $strGet = '')
{
$result = dbQuery($sql);
$pagingLink = '';
$totalResults = dbNumRows($result);
$totalPages = ceil($totalResults / $itemPerPage);

// how many link pages to show
$numLinks = 10;


// create the paging links only if we have more than one page of results
if ($totalPages > 1) {

$self = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] ;


if (isset($_GET['page']) && (int)$_GET['page'] > 0) {
$pageNumber = (int)$_GET['page'];
} else {
$pageNumber = 1;
}

// print 'previous' link only if we're not
// on page one
if ($pageNumber > 1) {
$page = $pageNumber - 1;
if ($page > 1) {
$prev = " <a href=\"$self?page=$page&$strGet/\">[Prev]</a> ";
} else {
$prev = " <a href=\"$self?$strGet\">[Prev]</a> ";
}

$first = " <a href=\"$self?$strGet\">[First]</a> ";
} else {
$prev = ''; // we're on page one, don't show 'previous' link
$first = ''; // nor 'first page' link
}

// print 'next' link only if we're not
// on the last page
if ($pageNumber < $totalPages) {
$page = $pageNumber + 1;
$next = " <a href=\"$self?page=$page&$strGet\">[Next]</a> ";
$last = " <a href=\"$self?page=$totalPages&$strGet\">[Last]</a> ";
} else {
$next = ''; // we're on the last page, don't show 'next' link
$last = ''; // nor 'last page' link
}

$start = $pageNumber - ($pageNumber % $numLinks) + 1;
$end = $start + $numLinks - 1;

$end = min($totalPages, $end);

$pagingLink = array();
for($page = $start; $page <= $end; $page++) {
if ($page == $pageNumber) {
$pagingLink[] = " $page "; // no need to create a link to current page
} else {
if ($page == 1) {
$pagingLink[] = " <a href=\"$self?$strGet\">$page</a> ";
} else {
$pagingLink[] = " <a href=\"$self?page=$page&$strGet\">$page</a> ";
}
}

}

$pagingLink = implode(' | ', $pagingLink);

// return the page navigation link
$pagingLink = $first . $prev . $pagingLink . $next . $last;
}

return $pagingLink;
}
?>

djr33
12-31-2009, 10:05 PM
You cannot start a session after you have output ANY text.
<html><?php session_start(); ?> -- ERROR!
<?php session_start(); ?></html> -- no error

You MUST redesign your system so that you can use session_start() before anything is output. There is NO way around this.

If you are using session_start() twice in a single page load (even in multiple included pages), then you are probably designing it wrong.
The only time you should use session_start() twice is when you start once, then you determine that you need to restart the session, such as if a user has logged out-- THEN you would use it again. But still, you must do ALL session_start() commands before any text it output from php.

If you have two pages that sometimes interact and sometimes run separately and on both you need session_start(), that is fine:
For both, you can do a conditional statement to determine if a session has been started:
if (session_id()=='') { session_start(); }
(If there is no ID for the session-- if there is no session-- then start a session).

Just post that code at the top of both (all) of your pages and then you should be fine.


Also, when you are working with includes, the code within an include effectively becomes part of the main page. So if you are worried about needing to use sessions in multiple included pages, there is no need. Just start the session in the main page (or any page-- but before any text is output), then it will be available throughout all of the processing of all included pages.


----
Now, on to the problem with the links:
function getPagingLink($sql, $itemPerPage = 10, $strGet = '')
That line sets $strGet by default to an empty string (nothing). That value is used to generate all of the URLs.
All you need to do is properly set it, so that it is not the default (blank).
When you call the function getPagingLink(...,...,$strGet), $strGet should be the string you want: "search=a". You can do this by using this, probably: $strGet = 'search='.$_GET['search'];

kid4mat
01-01-2010, 12:21 AM
for sesstion_start() , from djr33's advise, you should move it on the top of the page.

for error paging, I think you should use $_SESSION to store the search values in search page then retrieve it in common1.php,

by the way, as djr33 said $strGet by default to an empty string (nothing). That value is used to generate all of the URLs. Here you should put the string "string" in $strGet:


$strGet = 'search'

and you add =$_SESSION after the page links such as:


$self?page=$page&$strGet=$_SESSION

djr33
01-01-2010, 08:52 AM
I'm not sure about "adding $_SESSION". $_SESSION is an array. It may contain a lot of values you do not need.
You could use $_SESSION['variablename'] to send a specific value, or you could use session_id() to forward the session if you're thinking that cookies won't work, but that is usually handled by the browser.

However, in general, using sessions to store the values is a good idea, so that you don't need to worry about forwarding the value to every new page as a get value. The problem with that, though, is determining when the value is still current-- when do you delete it from the session, when do you use a new value instead-- what if there is a new get variable? From there, things can get a little complex.

thientanchuong
01-04-2010, 11:48 AM
Thank you both of you, I fixed my problem