php table array direction - php

<div class="section" id="section2">
<div class="slide" id="slide1">
<div class="box">
<div class="haha1">Library</div>
<p>
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =5 ;
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
}
?>
</table>
<br>
<center>Page 1</center>
</div></div>
<?php
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result))
{
?>
<table class="box1" width="700" border="1">
<tr>
<td><img src="<?php echo $row['image']; ?>" height="250" width="150"><br>
<?php echo $row['bookname']; ?><br>
<?php echo $row['author']; ?><br>
<input type="submit" name="add" value="Add Cart"></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
<?php } ?>
This is my table when I add new item list down vertically but I want array item horizontally example [item1,item2,item3,item4] after array 4 item horizontally only create another td then array again horizontally 4 time.
Required output:
{1,2,3,4}
{5,6,7,8}
array from left to right
else array vertically {1,5} {2,6},{3,7],[4,8]

try change the table this way
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =5 ;
$sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
}
?>
</table>
for testing the echo try a simple case like this (only the button)
echo "<td><input type='submit' name='add'
value='Add Cart'></td>";
In this sample (I have not you db an your IMG so i have change the var value with string value you can see 2 row of pic 4 in the first and 2 in the second.
Then if you don'obtain the same result could be you have some problem with your select or your value..
<table class="box1" width="700" border="1">
<?php
$cnt= 0;
$nThumbXRow =4 ;
$book = 0;
/* $sql = ("SELECT * FROM bookstore");
$result = mysql_query($sql);
while($row =mysql_fetch_array($result)){*/
while( $book<=5 ){
if ($cnt%$nThumbXRow == 0){
echo "<tr>";
}
/* echo "<td><img src='" . $row['image'] .
"' height='250' width='150'><br />" .
$row['bookname'] ."<br />" .
$row['author'] .
" <br /><input type='submit' name='add'
value='Add Cart'></td>";*/
echo "<td><img src='" . "row['image']" .
"' height='250' width='150'><br />" ."row['bookname']<br />" . "row['author']<br />" . "<input type='submit' name='add'
value='Add Cart'></td>";
$cnt++;
if ($cnt%$nThumbXRow == 0){
echo "</tr><!-- row ending here -->";
}
$book++;
}
?>
</table>

Related

Pagination not working-PHP

When I put a term in search-box, my pagination is not working although it gives me a perfect result.
For an eg. If I type "Laptops" it gives me records having laptops,but my pagination is not going with the flow. I don't know where I have missed something.
Here is my code:
Search-Box:
<form action="" method="GET">
Search: <input type="text" name="term" value="<?php echo #$_REQUEST['term']; ?>" /><br />
<input type="submit" value="Submit" />
</form>
To Display data:
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products WHERE status='Active' LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Image</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<!--<th>Category</th>
<th>Subcategory</th>-->
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term']))
{
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or status LIKE '%" . $term . "' or quantity LIKE '%" . $term . "' or details LIKE '%" . $term . "' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1)
{
while ($row = mysql_fetch_array($r_query))
{
echo "<tr bgcolor=''>";
echo "<td width='5%'>" . $row['id'] . "</td>";?>
<td width="10%"><img style='border:#666 1px solid;' width='70' src="<?php echo $row["productimage"]; ?>" alt="" /></td>
<?php echo "<td width='20%'>" . $row['product_name'] . "</td>";
echo "<td width='5%'>" . $row['price'] . "</td>";
echo "<td width='5%'>" . $row['status'] . "</td>";
echo "<td width='5%'>" . $row['quantity'] . "</td>";
echo "<td width='19%'>" . $row['details'] . "</td>";
// echo "<td>" . $row['category'] . "</td>";
// echo "<td>" . $row['subcategory'] . "</td>";
echo "<td width='10%'>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";?>
<td><a href="#" class="delete" onclick="dialogbox(<?php echo $row['id']; ?>)" >Delete</a>
<?php
//echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "' onclick='return show_confirm();' >Delete</a></td><tr>";
echo "</tr>";
}
}
else {
echo "Nothing should be displayed";
}
?>
</table>
Pagination Code:
<?php
//Pagination code starts here
$sql = "SELECT * FROM products";
$rs_result = mysql_query($sql); //run the query
$total_records = mysql_num_rows($rs_result); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='product_listing.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>
You have to use r_query to find no of rows in pagination. Now you are finding the total number of rows in the table
<?php
//Pagination code starts here
$total_records = mysql_num_rows($r_query); //count number of records
$total_pages = ceil($total_records / $num_rec_per_page);
echo "<a href='product_listing.php?page=1'>".'|<'."</a> "; // Goto 1st page
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='product_listing.php?page=".$i."'>".$i."</a> ";
};
echo "<a href='product_listing.php?page=$total_pages'>".'>|'."</a> "; // Goto last page
?>

Return only searched rows in table and hide other

If I write something in search box and press search , it should only return matched rows and hide other rows.
Here is my code, it works perfects only issue is it gives me searched record + all record list of table.
What can I do to show only searched data in table.?
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page=5;
if (isset($_GET["page"]))
{
$page = $_GET["page"];
}
else
{
$page=1;
}
$start_from = ($page-1) * $num_rec_per_page;
$result= mysql_query("SELECT * FROM products LIMIT $start_from, $num_rec_per_page");
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%".$term."%' or price LIKE '%".$term."' or details LIKE '%".$term."'";
$r_query = mysql_query($sql);
if($r_query>1)
{
while ($row = mysql_fetch_array($r_query)){
echo "<tr bgcolor='red'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
}
else{
echo "Nothing should be displayed";
}
}
?>
<?php
while($row=mysql_fetch_array($result))
{
echo "<tr class='danger'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['product_name']."</td>";
echo "<td>".$row['price']."</td>";
echo "<td>".$row['status']."</td>";
echo "<td>".$row['quantity']."</td>";
echo "<td>".$row['details']."</td>";
echo "<td>".$row['category']."</td>";
echo "<td>".$row['subcategory']."</td>";
echo "<td>".$row['date_added']."</td>";
echo "<td><a href='product_listing_edit.php?id=".$row['id']."'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=".$row['id']."'>Delete</a></td><tr>";
echo "</tr>";
}
?>
</table>
Just keep single while loop and run the query and search query as shown below it will solve the issue:
<div id="pageContent"><br />
<div class="search" align="right">
<form action="" method="post">
Search: <input type="text" name="term" /><br />
<input type="submit" value="Submit" />
</form>
</div>
<div class="container">
<table id="employee-grid" width="auto" cellpadding="1" cellspacing="1" border="1" class="table table-hover">
<?php
include_once '../storescripts/connect_to_mysql.php';
$num_rec_per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
};
$start_from = ($page - 1) * $num_rec_per_page;
$sql = "SELECT * FROM products LIMIT $start_from, $num_rec_per_page";
?>
<thead>
<tr class="success">
<th>Id</th>
<th>Product Name</th>
<th>Price</th>
<th>Status</th>
<th>Quantity</th>
<th>Details</th>
<th>Category</th>
<th>Subcategory</th>
<th>Date Added</th>
<th colspan="2">Functions</th>
</tr>
</thead>
<?php
if (!empty($_REQUEST['term'])) {
$term = mysql_real_escape_string($_REQUEST['term']);
$sql = "SELECT * FROM products WHERE product_name LIKE '%" . $term . "%' or price LIKE '%" . $term . "' or details LIKE '%" . $term . "'";
}
$r_query = mysql_query($sql);
if ($r_query > 1) {
while ($row = mysql_fetch_array($r_query)) {
echo "<tr bgcolor='red'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td>" . $row['details'] . "</td>";
echo "<td>" . $row['category'] . "</td>";
echo "<td>" . $row['subcategory'] . "</td>";
echo "<td>" . $row['date_added'] . "</td>";
echo "<td><a href='product_listing_edit.php?id=" . $row['id'] . "'>Edit</a></td>";
echo "<td><a name='delete' href='product_listing_delete.php?id=" . $row['id'] . "'>Delete</a></td><tr>";
echo "</tr>";
}
} else {
echo "Nothing should be displayed";
}
?>
</table>

Not deleting values from Database using checkboxes

I have a quick question, I'm trying to use a function to delete a certain movie from a database using checkboxes. I can add stuff to the database just fine, but can't seem to delete using the checkboxes. Thanks in advance for your help!
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
</head>
<body>
<?
$db = new PDO("mysql:host=localhost;dbname=armstrongpz", "armstrongpz", "12345");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
?>
<?
function deleteFromDatabase($db)
{
$deleteQuery = $db->prepare("SELECT * FROM movie");
$deleteQuery->execute();
foreach($deleteQuery->fetchAll() as $row)
{
if(array_key_exists($row['id'], $_POST))
{
$deleteQuery = $db->prepare('DELETE FROM movie WHERE movie.id='. $row['id']);
$deleteQuery->excute();
}
}
}
?>
<h2 style='text-align: center';>
Movie Collection Database
</h2>
<?
if($_SERVER['REQUEST_METHOD'] === "POST")
{
deleteFromDatabase($db);
if(isset($_POST['add']))
{
$TitleOfMovie = $_POST['TitleMovie'];
$StudioOfMovie = $_POST['StudioMovie'];
$RatingOfMovie = $_POST['mRating'];
$PubYearOfMovie = $_POST['PubYear'];
$IMDBRating = floatval($_POST['imdbRating']);
$RunTimeOfMovie = $_POST['RunTime'];
$addQuery = "INSERT INTO movie (id,title,studio,rating,pub_year,imdb_rating,run_time) VALUES(".'NULL'.", '$TitleOfMovie','$StudioOfMovie','$RatingOfMovie','$PubYearOfMovie', '$IMDBRating', '$RunTimeOfMovie')";
$statement = $db->prepare($addQuery);
$statement->execute();
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method='post'>
<table align='center' border='0' cellpadding='5'>
<tbody>
<tr>
<td style='border-style: none'>
<input name='TitleMovie' size='50' type='text' width='35' value= 'Title'/>
</td>
<td style='border-style: none'>
<input name='StudioMovie' size='25' type='text' width='35' value= 'Studio Name'/>
</td>
<td>
<?
$rating = array(1=>'G', 'PG', 'PG-13', 'R', 'NC-17', 'Not Rated');
echo "<select size='1' selected='movieRating' name='mRating'>";
foreach($rating as $key=>$value)
{
echo "<option value=\"$key\">$value</option>\n";
}
echo "</select>\n";
?>
</td>
<td style='border-style: none'>
<input name='PubYear' size='10' type='text' width='35' value='Pub Year'/>
</td>
<td style='border-style: none'>
<input name='imdbRating' size='10' type='text' width='35' value='IMDB rating'/>
</td>
<td>
<input name='RunTime' size='10' type='text' width='35' value='Run time'/>
</td>
<td>
<input type="checkbox" name="add" value="Add" align="top"/>Add
</td>
</tr>
<div style="text-align:center">
<input type='submit' name='submit' value='Update Database'>
</div>
</tbody>
</table>
</form>
<table border ='1' align='center'>
<?
$arrayOfFieldNames = array("Title", "Studio", "Rating", "Pub. Year", "IMDB Rating", "Run time(min)");
echo "<tr>";
echo "<td>". $arrayOfFieldNames[0] . "</td>";
echo "<td>". $arrayOfFieldNames[1] . "</td>";
echo "<td>". $arrayOfFieldNames[2] . "</td>";
echo "<td>". $arrayOfFieldNames[3] . "</td>";
echo "<td>". $arrayOfFieldNames[4] . "</td>";
echo "<td>". $arrayOfFieldNames[5] . "</td>";
echo "<td>Delete</td>";
echo "</tr>";
?>
<?
$query = "SELECT * FROM movie";
$stmt = $db->prepare($query);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>" .$row['title']. "</td>";
echo "<td>" .$row['studio']. "</td>";
echo "<td>" .$row['rating']. "</td>";
echo "<td>" .$row['pub_year']. "</td>";
echo "<td>" .$row['imdb_rating']. "</td>";
echo "<td>" .$row['run_time']. "</td>";
echo "<td><input type='checkbox' name='". $row['id'] . "' value='" . $row['id'] . "'>Delete</td>";
echo "</tr>";
}
?>
</table>
<?
if(isset($db))
{
$db= null;
}
?>
</body>

link not redirecting to another page

i have a table where one column is for edit button the code for it is
echo "<td><input type='image' src='images/icn_edit.png' title='Edit'></td> ";
i don't know why but the link is not working. Can anyone tell me whats wrong with it
the code for entire table is
<form method="post" action="sendmail.php">
<div class="tab_container">
<div id="tab1" class="tab_content">
<?php
$reload = $_SERVER['PHP_SELF'] . "?tpages=" . $tpages;
echo "<table class='tablesorter' cellspacing='0'> ";
echo "<thead>
<tr>
<th></th>
<th></th>
<th>Shopname</th>
<th>Instagram account</th>
<th>Favourite</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>";
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
mysqli_data_seek($result, $i);
$data = mysqli_fetch_assoc($result);
echo "<tr " . $cls . ">";
echo '<td></td>';
?><td> <input name="checkbox[<?php echo $data['id']?>]" type="checkbox"> </td>
<?
echo '<td>' . $data['fullname_shopname'] . '</td>';
echo '<td>' . $data['username_instagramacnt'] . '</td>';
if($data['staffpick']=='yes')
{
echo '<td>Yes</td>';
}
else
{
echo '<td>No</td>';
}
echo "<td><input type='image' src='images/icn_edit.png' title='Edit'></td> ";
echo "<td><a onclick=\"return confirm('delete this record?')\" href=\"a_del_vendor.php?id=".$data['id']."\" ><input type='image' src='images/icn_trash.png' title='Trash'></a></td> ";
echo "</tr>";
}
echo "</table>";
echo '<div class="pagination" style="margin-left:300px;" >';
if ($total_pages > 1)
{
echo paginate($reload, $show_page, $total_pages);
}
echo "</ul>
</div>";
?>
</div>
</div>
<footer>
<div class="submit_link">
<input type="submit" value="Send Mail" name="submit" class="alt_btn">
</div>
</footer>
</form>
If your <table> is wrapped in a <form> tag. Use an <img /> tag instead. Using <input type="image" /> will actually submit the form rather than to perform a link redirection.
echo "
<td>
<a href=\"a_add_fav_vendor.php?id=".$data['id']."\">
<img src=\"images/icn_edit.png\" title=\"Edit\"
</a>
</td>
";
Try this :
echo '<td><input type="image" src="images/icn_edit.png" title="Edit"></td>';
Do some Change like
echo "<td><a href='a_add_fav_vendor.php?id=".$data['id']."'><input type='image' src='images/icn_edit.png' title='Edit'></a></td> ";

post value's text field OrderForm

I have multiple books that people can order. All those books are stored in a MySQL database.
People can enter value's (INT) into a textfield. Example: Book One value = [5].
But when I enter submit it will only show the last entered value of that textfield.
How can I arrange, that if people only enter value's in some textfields and then hit submit they see what product they ordered and the value with it. Thanks :)
My code
<table width="990">
<form name="form" action="<?php $_SERVER['PHP_SELF']; ?>" method="post">
<tr>
<td width="93" class="main">Bestelnummer</td>
<td width="550" class="main">Titel</td>
<td width="100" class="main">Categorie</td>
<td width="150" class="main">Type Onderwijs</td>
<td width="80" class="main">Groep</td>
<td width="50" class="main">Prijs</td>
<td width="40" class="main">Aantal</td>
</tr>
<?php
// Laat Resultaten zien
$s = "SELECT * FROM producten ORDER BY id ASC";
$sql = (mysql_query($s))or die ("FOUT: " . mysql_error());
while($row = mysql_fetch_array($sql))
{
$id = $row['id'];
echo "<tr>";
echo "<td width='93'>" .$row['bestelnummer']. "</td>";
echo "<td width='550'><a href='".$row['link']."' target='_blank' title='".$row['titel']."' >" .$row['titel']. "</a></td>";
echo "<td width='100'>" .$row['categorie']. "</td>";
if ($row['onderwijs'] == "BO") { echo "<td width='150'>Basis Onderwijs</td>"; } elseif ($row['onderwijs'] == "VO") { echo "<td width='150'>Voortgezet Onderwijs</td>"; } else { }
echo "<td width='80'>" . $row['groep'] . "</td>";
if ($row['prijs'] == 0) { echo "<td width='50'><i>gratis</i></td>"; } else { echo "<td width='50'>€ " .$row['prijs']. "</td>"; }
echo "<td width='40'><input type='text' name='nummer".$id."' title='nummer".$id."' class='aantal' maxlength='4' /></td>";
echo "</tr>";
}
?>
<tr>
<td><input type="submit" name="submit" value="Plaats bestelling" class="verzend"/></td>
</tr>
</form>
</table>
<?php if (isset($_POST['submit']))
{
if (empty($_POST['aantal']))
{
echo "L33g";
}
else
{
$_POST['nummer".$id."'];
}
}?>
You would be better off using an array so use
<input type='text' name='nummer[".$id."]' title='nummer".$id."' class='aantal' maxlength='4' />
Then replace
$_POST['nummer".$id."'];
with
foreach($_POST['nummer'] as $value) {
echo $value;
}

Categories