•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,993 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,276 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 680 | Replies: 25 | Solved
![]() |
•
•
Join Date: Apr 2008
Posts: 48
Reputation:
Rep Power: 1
Solved Threads: 3
•
•
•
•
Actually am using this for search.In my db I am storing few words A-Z but not in alphbetic order.so when i wanted to search 'A',then all data from my db related to 'A' must display.Here am able to display A1 to A10 in 1st page.In 2nd page it should start with A11 but it is displaying the data from my db whose Id=11.Am I clear to you now?
I see, that makes a bit more sense now. So 11 could be K or something.. Well, I think your search is working perfectly (that I know of) but you're using LIKE %$trimmed%
Let's say that $trimmed is your search for A, it should return A1 - A10 then A11-A20 (or w/e) then if where id 11 dispname is TALC 'N' POWDER - it'll pick it up because it has an A in it. the %A% means anything before the A and anything after the A so TALC would get in but ZINC wouldn't. So if you just want to narrow your search to A--- on disp name.
Use this:
php Syntax (Toggle Plain Text)
<?php include ('conn.php'); // if $_GET['page'] defined, use it as page number if(isset($_GET['page'])) { $page = $_GET['page']; } else { // if $_GET['page'] is not defined then $page == 1 $page = 1; } // counting the offset //$searchresult = array(); $var = $_POST['keyword'] ; $trimmed = mysql_real_escape_string(trim($var)); // rows to return $str = "SELECT * FROM `medlist` WHERE `dispname` LIKE '$trimmed%'"; echo $str.'<br>'; $res= mysql_query($str); if(!$res){die(mysql_error());} $num_rows= mysql_num_rows($res); echo $num_rows.'<br>'; // how many rows to show per page $rowsPerPage =10; $maxPage = ceil($num_rows/$rowsPerPage); //$page=(isset($_GET['page']))?$_GET['page']:1; //$offset=($page-1)*$rowsPerPage; if ($page < 1) { $page = 1; } elseif ($page > $maxPage) { $page = $maxPage; } $max = 'LIMIT ' . ($page - 1) * $rowsPerPage . ',' . $rowsPerPage; $query = "SELECT * FROM `medlist` WHERE `dispname` LIKE '$trimmed%' $max"; $result = mysql_query($query); //<a href=page.php?search=$string&offset=$offset> if(!$result){die(mysql_error());} while($data = mysql_fetch_array($result)) { echo ' <tr> <td colspan="3" > <a href="'.$data['MedName'].'" >' . $data['dispname'].' </a> </td>'; echo'</tr>'; } echo "<tr><td align='left' colspan='2'>Result Pages:"; for($i=1;$i<=$maxPage;$i++) { echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i>".$i." | "; } echo "</td> <td>Showing page <strong>".$page."</strong> of <strong>".$maxPage."</strong> pages </td> </tr>"; ?>
Does that help?
•
•
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 536
Reputation:
Rep Power: 3
Solved Threads: 54
so what exactly was wrong with the code i typed up for you.
the reason you getting the problem like i told you earlier is that you are not sending the search term along with each link in the script. the $_POST['keyword'] which contains the term on the first page is reset once you click a link. making it not show the same results from before with the different limit.
here is my code i made for you:
(others, please check for errors. i haven't found any, but i like to miss little things.)
the reason you getting the problem like i told you earlier is that you are not sending the search term along with each link in the script. the $_POST['keyword'] which contains the term on the first page is reset once you click a link. making it not show the same results from before with the different limit.
here is my code i made for you:
(others, please check for errors. i haven't found any, but i like to miss little things.)
<?php
if (isset($_POST['submit'])) {
$var = trim($_POST['keyword']);
header('Location: pageno.php?keyword=' . $var);
die();
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PgNo</title>
<link href="links.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="1" width="100%">
<tr>
<td align="center">
<form method="post" action="pagno.php">
<input type="text" size="40%" id="keyword" name="keyword" />
<input type="submit" name="submit" value="Search" />
</form>
</td>
</tr>
<tr>
<td class="imag" id="def">| <a href="home.html"> Home </a> | <a href="dc.php"> D & C </a>| <a href="st.php"> S & T</a> | <a href="medi.php"> Med</a> |<a href="hc.html"> HC</a> |<a href="news.html"> News </a>|</td>
</tr>
</table>
<table border="1" align="center">
<tr>
<th align="left"> Search Results :</th>
</tr>
<tr>
<td align="center">
<?php
//make sure you add your database connection here
//Set Rows Per Page
$rowsPage = 10;
if (isset($_GET['keyword'])) {
$word = mysql_real_escape_string($_GET['keyword']);
if ($word !== '') {
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page = 1;
}
$start = ($rowsPage * $page) - $rowsPage;
$sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'";
$query = mysql_query($sql);
$total = mysql_num_rows($query);
$pages = ceil($total/$rowsPage);
if ($pages > 1) {
$limit = ' LIMIT ' . $start . ', ' . $rowsPage;
}
else {
$limit = '';
}
$sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'" . $limit;
$query = mysql_query($sql);
echo '<table border="0" cellspacing="0" cellpadding="3">';
while ($row = mysql_fetch_assoc($query)) {
echo '<tr>';
echo '<td><a href="' . $row['MedName'] . '">' . $row['dispname'] . '</a></td>';
echo '</tr>';
}
echo '</table>';
echo '<center>Pages: ';
$i = 1;
while ($i < count($pages)) {
echo '<a href="pagno.php?keyword=' . $word . '&page=' . $i . '">' . ($page == $i ? '<font color="#FF0000">' . $i . '</font>' : $i) . '</a> ';
$i++;
}
echo '</center>';
}
}
?>
</td>
</tr>
</table>
</body>
</html> Last edited by kkeith29 : Jun 4th, 2008 at 1:11 am.
•
•
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation:
Rep Power: 1
Solved Threads: 0
Sam,In the code we have I found the problem.We have 2 queries
When I print them,In 1st page they are printing :
In 2nd page they are printing
So the problem here is in 2nd page the keyword is not there so it is displaying Id 11 from db.I changed the a href tag which is in the for loop :
I have added keyword and offset values but even this doesnt bring any change in the output.
What can be done next?
$str = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%".$trimmed."%'"; $query = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%".$trimmed."%'".$max ;
SELECT * FROM `medlist` WHERE `dispname` LIKE '%a%' & SELECT * FROM `medlist` WHERE `dispname` LIKE '%a%' limit 0,10
SELECT * FROM `medlist` WHERE `dispname` LIKE '%%' & SELECT * FROM `medlist` WHERE `dispname` LIKE '%%' limit 10,10
echo "<a href=pagno.php?rowsPerPage=$rowsPerPage&page=$i&trimmed=$trimmed&offset=$offset>".$i." | ";
What can be done next?
Suhasini
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 536
Reputation:
Rep Power: 3
Solved Threads: 54
finally i got sick of this stuff and tested it on my server.
i created the table and filled it with random values.
here is the test script url: http://www.banditssoftball.org/pagno.php
to find a result, type in a number into the search box.
i made a mistake on my last script i posted for you. i used count() for some reason, use to it i guess, when i was echoing out the pages.
i created the table and filled it with random values.
here is the test script url: http://www.banditssoftball.org/pagno.php
to find a result, type in a number into the search box.
i made a mistake on my last script i posted for you. i used count() for some reason, use to it i guess, when i was echoing out the pages.
Last edited by kkeith29 : Jun 4th, 2008 at 3:07 am.
•
•
Join Date: May 2008
Location: Hyderabad,India
Posts: 89
Reputation:
Rep Power: 1
Solved Threads: 0
•
•
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 536
Reputation:
Rep Power: 3
Solved Threads: 54
yes, the one i typed for you (with minor changes).
here it is:
here it is:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>PgNo</title>
<link href="links.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table border="1" width="100%">
<tr>
<td align="center">
<form method="get" action="pagno.php">
<input type="text" size="40%" id="keyword" name="keyword" />
<input type="submit" value="Search" />
</form>
</td>
</tr>
<tr>
<td class="imag" id="def">| <a href="home.html"> Home </a> | <a href="dc.php"> D & C </a>| <a href="st.php"> S & T</a> | <a href="medi.php"> Med</a> |<a href="hc.html"> HC</a> |<a href="news.html"> News </a>|</td>
</tr>
</table>
<table border="1" align="center">
<tr>
<th align="left"> Search Results :</th>
</tr>
<tr>
<td align="center">
<?php
//Make sure you input your database connection here
//Set Rows Per Page
$rowsPage = 10;
if (isset($_GET['keyword'])) {
$word = mysql_real_escape_string($_GET['keyword']);
if ($word !== '') {
if (isset($_GET['page'])) {
$page = $_GET['page'];
}
else {
$page = 1;
}
$start = ($rowsPage * $page) - $rowsPage;
$sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'";
$query = mysql_query($sql);
$total = mysql_num_rows($query);
$pages = ceil($total/$rowsPage);
if ($pages > 1) {
$limit = ' LIMIT ' . $start . ', ' . $rowsPage;
}
else {
$limit = '';
}
$sql = "SELECT * FROM `medlist` WHERE `dispname` LIKE '%" . $word . "%'" . $limit;
$query = mysql_query($sql);
echo '<table border="0" cellspacing="0" cellpadding="3">';
while ($row = mysql_fetch_assoc($query)) {
echo '<tr>';
echo '<td><a href="' . $row['MedName'] . '">' . $row['dispname'] . '</a></td>';
echo '</tr>';
}
echo '</table>';
echo '<center>Pages: ';
$i = 1;
while ($i <= $pages) {
echo '<a href="pagno.php?keyword=' . $word . '&page=' . $i . '">' . ($page == $i ? '<font color="#FF0000">' . $i . '</font>' : $i) . '</a> ';
$i++;
}
echo '</center>';
}
}
?>
</td>
</tr>
</table>
</body>
</html>![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
Similar Threads
- PHP Navigation help please (PHP)
- CSS position problem (HTML and CSS)
- Change IE "Action Canceled" page?? (Web Browsers)
- Scrolling tables problem (HTML and CSS)
- Router is slower than adapter (Network Security)
- Parse error at last line of page's code (PHP)
- Navigation Bar that extends the length of the page (HTML and CSS)
- Problem - IE Synchronize navigation (Web Browsers)
- Mouse over buttons on .js file? Urgent-help (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: 2 tier & 3 tier architectures in php?
- Next Thread: Serious problem with attchments-please help


Linear Mode