been stuck on this for hours.
I have a simple (I think) pagination PHP code here working fine for the first page - meaning I get the correct amount of pages displayed and the correct results displayed. Code:
<?php
$query = $_POST ['query'];
$link=mysql_connect('localhost','root','root');
mysql_select_db("asset_catalog",$link);
$q=("SELECT COUNT(*) \"total\" FROM assets WHERE Description LIKE '%$query%' OR Manufacturer LIKE '%$query%'");
$ros=mysql_query($q,$link) or die(mysqli_error());
$row=mysql_fetch_array($ros);
$total=$row['total'];
$dis=15;
$total_page=ceil($total/$dis);
$page_cur=(isset($_GET['page']))?$_GET['page']:1;
$k=($page_cur-1)*$dis;
$q=("SELECT * FROM assets WHERE Description LIKE '%$query%' OR Manufacturer LIKE '%$query%' limit $k,$dis");
$ros=mysql_query($q,$link);
echo "<table border='0'>
<tr>
</tr>";
while($row=mysql_fetch_array($ros))
{
if (($i % 5) == 0) echo "<tr>";
echo "<td><img src='".$row['Image']."' id='queryimg'><br>
<a href='details.php?ID=".$row['ID']."' style='color: #fff;'>{$row[Description]}</a></td>";
if (($i % 5) == 4) echo "</tr>";
$i++;
}
if ( $i > 0 && ($i-1) % 3 < 2) echo "</tr>";
echo "</table>";
?>
<div style="position: absolute; left: 320px;">
<?php
if($page_cur>1)
{
echo '';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:120px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value=" Previous ">';
}
for($i=1;$i<$total_page;$i++)
{
if($page_cur==$i)
{
echo ' <input style="background-color:green;border:2px black solid;border-radius:5px;width:30px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> ';
}
else
{
echo ' <input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:30px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value="'.$i.'"> ';
}
}
if($page_cur<$total_page)
{
echo '<input style="cursor:pointer;background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button" value=" Next ">';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-radius:5px;width:90px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button" value=" Next ">';
}?>
The problem is if I click on the second page it displays ALL results in the DB - not the results that should be in the second page. Additionally, if I have say 3 pages initially, once I go to the second page it shows me there are 70 pages aka(all the db)
I'm sure there is a quick fix for this but for the life of me I can't figure it out.
Any help, advice would be great. Thank you!
Between pages you lose your $_POST['query'] .
Instead of
$query = $_POST ['query'];
do
$query = (isset($_POST ['query']))?$_POST['query']:$_GET['query'];
or use
$query = $_REQUEST['query'];
AND
in your pagination link replace
'<a href="pagination.php?page='.($page_cur-1).'" style
with
'<a href="pagination.php?page='.($page_cur-1).'&query='.$query.'" style
Related
When i keep search field blank and hit on search button, then show all results from mysql database, Why.... here is my php code....
I want to create it, when i keep search field blank and click search button... have to show error "no search result" and want to create disallowed white spacing search and need extra one HTML button for all Entries. By one click so that i get all entries......
please help me....
<?php
$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');
if(isset($_POST['button'])){ //trigger button click
$search=$_POST['search'];
$query=mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");
if (mysql_num_rows($query) > 0) {
while ($row = mysql_fetch_array($query)) {
echo "<tbody>";
echo "<tr>";
echo "<td data-label='Student ID'>".$row['student_id']."</td>";
echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
echo "<td data-label='District'>".$row['district']."</td>";
echo "</tr>";
echo "</tbody>";
}
}else{
echo "<div class='error-text'>No results</div><br><br>";
}
}else{ //while not in use of search returns all the values
$query=mysql_query("select * from iconic19");
while ($row = mysql_fetch_array($query)) {
}
}
mysql_close();
?>
Its Html Code
<form id="nbc-searchblue1" method="post" enctype="multipart/form-data" autocomplete="off">
<input id='wc-searchblueinput1' placeholder="Search Iconic..." name="search" type="search" autofocus>
<br>
<input id='nbc-searchbluesubmit1' value="Search" type="submit" name="button">
<div class="view-all"> Show all</div>
</form>
Its css Code..
.view-all a {
background: red;
padding: 10px;
border-radius: 4px;
color: #fff;
text-decoration: none;
}
<?php
$con=mysql_connect('localhost', '1093913', 'tanim1996');
$db=mysql_select_db('1093913');
if(isset($_POST['button'])){ //trigger button click
$numRows = 0;
if(!empty($_POST['search'])) {
$search = mysql_real_escape_string($_POST['search']);
$query = mysql_query("select * from iconic19 where student_id like '%{$search}%' || name like '%{$search}%' || phone like '%{$search}%' || blood like '%{$search}%' || district like '%{$search}%' ");
$numRows = (int)mysql_num_rows($query);
}
if ($numRows > 0) {
while ($row = mysql_fetch_array($query)) {
echo "<tbody>";
echo "<tr>";
echo "<td data-label='Student ID'>".$row['student_id']."</td>";
echo "<td data-label='Name' style='font-weight:bold;' >".$row['name']."</td>";
echo "<td data-label='Mobile No'>"."<a href='tel:".$row['phone']."'>".$row['phone']."</a>"."</td>";
echo "<td data-label='Blood' style='color:red; font-weight:bold;' >".$row['blood']."</td>";
echo "<td data-label='Email'>"."<a href='mailto:".$row['email']."'>".$row['email']."</a>"."</td>";
echo "<td data-label='District'>".$row['district']."</td>";
echo "</tr>";
echo "</tbody>";
}
} else {
echo "<div class='error-text'>No results</div><br><br>";
}
} else { //while not in use of search returns all the values
$query = mysql_query("select * from iconic19");
while ($row = mysql_fetch_array($query)) {
}
}
mysql_close();
?>
What I have done was creating a new variable $numRows with a default value of 0. If your search is empty there is no query to the database. I escaped your $search variable.
BTW: Please change to mysqli, the mysql extension is no longer supported in newer php versions.
Just check if $_POST['search'] is blank then display your message else execute your query.
I am bit new to php and sql.
I am simply reading from a database table and echoing images to a page. The images are then styled with a text aligned in the middle saying 'Play Video'.
In the table 'adcTable' some entries do not have a video stored.
I would like to have the rows/entries with video say 'Play Video' and the ones without just show the image.
Not to sure how bess to explain. Hope to get some assistance.
<php
$sql = "SELECT client_id, images, video FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["images"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Play Video</div>
</div>
</div>
</td>";
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
echo "</tr></table>";
?>
Thanks guys, I was able to use the example provided by BusinessPlanQuickBuilder to solve.
$sql = "SELECT client_id, files, file FROM adcTable";
$result = $conn->query($sql);
$count = 1;
echo "<table border = '0' width='720'><tr>";
while($row = $result->fetch_assoc()) {
if ($row['file'] == "VidUploads/"){
echo "<td>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
</td>";
echo '<script type="text/javascript">',
'$( ".text" ).css( "border", "3px solid red" );',
'</script>';
} else{
echo "<td><div class='ccontainer'>"; ?><img class="cimage" src= "<?php echo $row ["files"];?> " ><?php echo "
<div class='middle'>
<div class='text'>Video</div>
</div>
</div>
</td>";
}
if ($count++ % 2 == 0) {
echo "</tr><tr>";
}
}
echo "</tr></table>";
?>
Use if empty condition on video column. This is the fundamental code, add your formatting as required.
<?php
while($row = $result->fetch_assoc()) {
if (empty ($row['video']) ) {
echo $row ["images"];
} else {
echo "Play Video";
}
}
?>
SO reference to related post
I have been trying to set up a page that lists prices of items from a table in a database. Here is my code:
<?php
$querylist = mysql_query("SELECT item_name,image,price,added_by FROM values");
while($row = mysql_fetch_array($querylist))
{
echo '<div class="post rareitem" style="margin-right: 15px;float: left;">
<div class="rarename">
<strong>';
// Shows Item Name
echo $row['item_name'];
echo '</strong>
</div>';
// Shows Item Image
echo '<div class="rareimage" style="background-image: url(/app/tpl/skins/Mango/images/values/rares/';
echo $row['image'];
echo ');"></div>';
// Shows Item Price
echo '<div class="rarecontrols">
<div class="coinsbox"></div>
<span>
<b> <b>Credits: </b> </b> ';
echo $row['price'];
echo '</span>';
// Shows Who Added the Item
echo '<div class="addedbox"></div><b><b><span><font color="#c93734"><font color="#c93734">Added By: </font> </font>';
echo $row['added_by'];
echo '</span></b></b>
</div>
<div class="clear"></div>
</div>';
}
?>
There is another chunk of code (shown below) that I have based this off of, and it works perfectly fine. I can't seem to get this to work though. I believe it has something to do with the SQL, the syntax, or something. No matter what I do, it produces absolutely no results, yet the code below results exactly as planned. I know for a fact it is not a connection issue because the below code can be placed on the same exact page as the above one and it works fine, however the above does not.
<?php
$querylist = mysql_query("SELECT id,username,motto,country,look,rank,account_created,role,account_created,online,last_online FROM users WHERE rank='9' ORDER BY ID LIMIT 20");
while($row = mysql_fetch_array($querylist))
{
echo '
<div class="team">';
// Showing Avatar
echo '<div style="float: left; margin-top:-1px;height: 60px; width: 64px; background: url(http://www.habbo.nl/habbo-imaging/avatarimage?figure=';
echo $row['look'];echo "&action=wav&direction=3&head_direction=3&gesture=sml&size=m) no-repeat 0px -10px";
echo "\"/>";
echo "<img alt=\"badge\" src=\"/app/tpl/skins/habbo/images/icons/";
echo $row['online'];echo ".gif\"></div>";
// Flags
echo "<br/><img src=\"/app/tpl/skins/habbo/images/icons/flags/";
echo $row['country'];echo ".png";
echo '" style="float:right;" /> <b><uf>';
echo $row['username'];echo "</u></b>";
// Bans & Ticket Count
$Bans = mysql_query("SELECT * FROM `bans` WHERE `added_by` = '" . $row['username'] . "'");
$BanCount = mysql_num_rows($Bans);
$Tickets = mysql_query("SELECT * FROM `moderation_tickets` WHERE `moderator_id` = '" . $row['id'] . "'");
$TicketCount = mysql_num_rows($Tickets);
//Role
echo "<br/><gb>Role: </b><fi> ";
echo $row['role'];echo "";
echo "</i>";
// Echoing bans & Tickets
echo "<br/><gb>Bans: </b><fi> ";
; echo $BanCount;
echo "</i>";
echo " <gb>Tickets: </b><if>";
; echo $TicketCount;
echo "</i>";
echo "</div>";
}
?>
Thanks in advanced, any assistance will be greatly appreciated!
values is Reserved Words in mysql it should be on backtick
SELECT item_name,image,price,added_by FROM `values`
And stop using mysql it is deprecated. Instead use mysqli or PDO
I have a web page with images and when user clicks on any of the image, it has to derive data of that particular image from MYSQL database. What I am doing is using a simple JavaScript popup and putting the data from database. However I am just getting the first item from database on all images.
This is the code:
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
while($row = mysql_fetch_array($result))
{
if ($colCnt%4==0)
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
$colCnt++;
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
$row['name'] is just giving out the first name as it is in a while loop. I am not being able to get other names for other images. How can this be done. Any help would be appreciated.
Does one iteration in your while fetch single image data? And what I can understand according to your code is that you are displaying 4 image in a row.
Can you please format your code a bit..its looking too ugly.
I need to know which statement is calling your modal window.
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=0;
$i = 0;
echo '<tr>';
while($row = mysql_fetch_array($result))
{
$num = $files[$i];
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br>
<div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow"><div><p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>Ok</div>
</div></td>';
$colCnt++;
if ($colCnt % 4 == 0)
{
echo '</tr>';
$colCnt=0;
}
$i++;
}
mysql_close($con);
include 'footer.php';
?>
Try this.
Also see how beautiful the code looks if its properly formatted..
try this
<?php
$files = glob("admin/images/paintings/*.*");
echo '<div id="painting"><table border="0" style="width:590px;">';
$colCnt=4;
while($row = mysql_fetch_array($result))
{
for ($i = 0; $i < $colCnt; $i++) {
echo '<tr>';
echo '<td width="25%" style="font-size:8.5px; font-family:arial">';
echo($i);
$num = $files[$i];
echo '<img id="indPainting" src="'.$num.'" align="absmiddle" /> <br> <div id="paintingName">';
print $row['name'];
echo '<div id="openModal" class="modalWindow">
<div>
<p>This is a sample modal window that can be created using CSS3 and HTML5.'.$row['name'].'</p>
Ok
</div>
</div>';
echo '</td>';
}
if ($colCnt==4)
{
echo '</tr>';
$colCnt=0;
}
}
mysql_close($con);
include 'footer.php';
?>
I'm trying to find a way to upload a value to SQL using a checkbox but no luck
this is my code:
/////////////////////////// FOR RXTRA ////////////////////////////////////////////////////////////
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > $i) {
$ID = mysql_result($result,$i,"ext_id");
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");
$DES= mysql_result($result,$i,"ext_description");
//this part chack if the value is "0" and show with "tooltip" the value\/
if ( $PR == 0 ) {
print ''.$NA.'<span>' .' free '.'</span>!';
} else {
print ''. $NA .'<span>' .' add '.' '. $PR .' $ '. '</span>!';
}
print "<input style='width: 30px; height: 15px;' type='checkbox' name='extra[]' value='$NA'></td>\n";
//this java calculate the value add to extra ant outpot the total extra that pass to sql table
print "<input type='hidden' name='item_name'/>";
print "<input type='hidden' name='amount'/>";
print "<input style='width: 30px; height: 15px;' type='checkbox' onClick='ReadForm (this.form, false);' value='+$PR'></td>\n";
$i++;
}
?>
</div></div>
<?
} else {
}
?>
<!----->
<div class="item_add_cart">
<span class="title">total extra $</span>
<div class="content">
<?
print "<input style='color:#000;font-size:13px;' size='7' name='tot' type='text'/>";
?>
</div></div>
<!----->
my problem is that I have two table's one is the total price and one is the names I try to insert all value with one checkbox and it's not working
If I create 2 checkboxes and click on them then the value uploads ok
but I need only one checkbox that sends value from $NA to Table ext_name and total price from name='tot' to ext_price table
Please note that the above code doesn't work, #Majid Fouladpour suggests the following code instead.
/////////////////////////// FOR RXTRA ////////////////////////////////////////////////////////////
$sql = "SELECT ext_id,ext_price,ext_name,ext_description FROM tbl_extra ORDER by ext_id ASC";
$result = mysql_query($sql) or trigger_error(mysql_error(),E_USER_ERROR);
$number = mysql_num_rows($result);
$i = 0;
while ($number > > $i) {
$ID = mysql_result($result,$i,"ext_id");
$NA= mysql_result($result,$i,"ext_name");
$PR= mysql_result($result,$i,"ext_price");
$DES= mysql_result($result,$i,"ext_description");
//this part chack if the value is "0" and show with "tooltip" the value\/
if ( $PR == 0 ) {
print ''.$NA.'' <a href="#" class="tooltip2">'.$NA.'<span>' .' free '.'!';
.'</span></a>!';
} else {
print ''. <a href="#" class="tooltip2">'. $NA .'' '<span>' .' add '.' '. $PR .' $ '. '!';
</span></a>!';
}
print "\n";<input style='width: 30px; height: 15px;' type='checkbox' name='extra[]' value='$NA'></td>\n";
//this java calculate the value add to extra ant outpot the total extra that pass to sql table
print "";
<input type='hidden' name='item_name'/>";
print "";<input type='hidden' name='amount'/>";
print "\n";
<input style='width: 30px; height: 15px;' type='checkbox' onClick='ReadForm (this.form, false);' value='+$PR'></td>\n";
$i++;
}
?>
total >
</div></div>
<?
} else {
}
?>
<!----->
<div class="item_add_cart">
<span class="title">total extra $</span>
<div class="content";
>
<?
print "<input style='color:#000;font-size:13px;' size='7' name='tot' type='text'/>";
?>
>
</div></div>
<!----->
(I've made this answer community wiki so the rep doesn't go to me)