I need to display a photo gallery in a table, I want five pictures on each line, but can't find the way to insert </tr><tr> after each fifth picture.
Here's my code:
<?php
// table name
$tbl_name=gallery1;
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows= mysql_fetch_assoc($result)){
$id = $rows['id'];
$path = $rows['path'];
$image_name = $rows['image_name'];
$title = $rows['title'];
?>
<img src="<?php echo $path."/".$image_name;?>" height="120"/>Name:<?php echo $title;?>
<?php
echo "<form action='pictry.php' enctype='multipart/form-data' method='post'>
<input name='file[]' type='hidden' value='".$image_name."' />
<input name='id' type='hidden' id='id' value='".$id."'/>
<input type='submit' name='button' id='button' value='Delete Picture' /></form>";
}
?>
Replace the line
while($rows= mysql_fetch_assoc($result)){
with
for($i = 0; $rows= mysql_fetch_assoc($result); ++$i) {
Then you place something like this into the for loop.
if($i % 5 == 0) { /* insert stuff */ }
Untested Code
<?php
$perrow = 5;
$i = 0;
echo '<table>';
echo '<tr>';
while($rows = mysql_fetch_assoc($result)) {
echo '<td><img src=[grapresultfromrows] /></td>';
++$i;
if($i == $perrow) {
$i = 0;
echo '</tr>';
echo '<tr>';
}
}
// If not a multiple of $perrow you need to add extra cells
for($i; $i < $perrow; ++$i) {
echo '<td></td>';
}
echo '</tr>';
echo '</table>';
?>
Related
I need to break the line after a reach of 10 buttons.
Please be specific what are the code to be used for breaking the line.
Here is the code:
<?php
session_start();
require('connection.php');
$result = mysql_query("SELECT * FROM tbCandidates ");
if($result){
}
echo "select Project to Reward for";
$_SESSION['a']="project name-->";
echo "<body><form method='post' action='part1.php' ><input type='hidden' value='abhishek' name='aa'/>";
echo "<table><tr>";
while ($row=mysql_fetch_array($result)){
echo "<td>";
echo $row["project_id"];
echo '<input type="radio" name="n1" value="';echo $row["project_id"]; echo '">'; echo"</input>";
$_SESSION['b']=$row['candidate_name'];
$_SESSION['c']=$row['candidate_position'];
$_SESSION['c']=$row['candidate_nominee'];
echo "</td>";
}
echo "</tr></table><input type='submit'/></form></body>";
If you mean by BREAKING the line is creating another row for your table then try to use the following codes:
<?php
session_start();
require('connection.php');
$result = mysql_query("SELECT * FROM tbCandidates ");
if($result){
}
echo "select Project to Reward for";
$_SESSION['a']="project name-->";
echo "<body><form method='post' action='part1.php' ><input type='hidden' value='abhishek' name='aa'/>";
echo "<table>";
$counter = 0;
while ($row=mysql_fetch_array($result)){
$counter++;
if($counter==1){
echo "<tr>";
}
echo "<td>";
echo $row["project_id"];
echo '<input type="radio" name="n1" value="';echo $row["project_id"]; echo '">'; echo"</input>";
$_SESSION['b']=$row['candidate_name'];
$_SESSION['c']=$row['candidate_position'];
$_SESSION['c']=$row['candidate_nominee'];
echo "</td>";
if($counter==10){
echo "</tr>";
$counter = 0;
}
}
echo "</table><input type='submit'/></form></body>";
Let me know if it works.
OK let me be as specific as possible.... I would like to change the way my search bar behaves. Right now the string has to match the MySQL query exactly!!! or it won't show my products.
Here is what I got:
A search for "case iphone"
searchitems.php?tosearch=case+iphone&Search=Search
Now a search for "iphone case"
searchitems.php?tosearch=iphone+case&Search=Search
Now based on how the query would work...if I type in the URL manually to this... it works how I want it to:
searchitems.php?tosearch=%iphone%&%case%&Search=Search
So how do I go about changing the URL from
searchitems.php?tosearch=case+iphone&Search=Search
to
searchitems.php?tosearch=%iphone%&%case%&Search=Search
Here is the code that I have so far:
Search Form In index.php
<form method="GET" action="searchitems.php">
<input size="50" type="text" name="tosearch" value="Search" name="keyword" id="keyword" title="keyword" onfocus="clearText(this)" onblur="clearText(this)" class="txt_field">
<input type="submit" name="Search" value="Search" alt="Search" id="searchbutton" title="Search" class="sub_btn">
</form>
searchitems.php (Sorry if it isn't tabbed correctly just copied and pasted)
<?php
include('core/header2.php');
include ('core/connectdb.php');
if(isset($_GET['tosearch'])) {
$tosearch=$_GET['tosearch'];
$tosearch=urldecode($tosearch);
$tosearch = preg_replace('!\s+!', ' ', trim($tosearch));
$search_terms = explode(' ',$tosearch);
$search_terms[] = $tosearch;
$search_terms=array_unique($search_terms);
$query = "select * from products where ";
$query_fields = Array();
$sql = "SHOW COLUMNS FROM products";
$columnlist = $connect->query($sql);
while($arr = $columnlist->fetch_assoc()){
extract($arr);
$query_fields[] = $Field . " LIKE ('%". $tosearch . "%')";
}
$query .= implode(" OR ", $query_fields);
$results = mysqli_query($connect, $query) or die(mysql_error());
$rows = $results->num_rows;
if ($rows > 0) {
$cols = 5;
$counter = 1;
$nbsp = $cols - ($rows % $cols);
echo '<div id="content" class="float_r">';
echo "<table border=\"0\">";
while ($row = mysqli_fetch_array($results, MYSQLI_ASSOC)) {
if(($counter % $cols) == 1) { // Check if it's new row
echo '<tr align="center">';
}
extract($row);
echo '<td valign="top" style="padding-right:15px;">';
echo "<a href=itemdetails.php?itemcode=$item_code>";
echo '<img src=' . $imagename . ' style="max-width:120px;max-height:140px;
width:auto;height:auto;"></img><br/>';
echo $item_name .'<br/>';
echo "</a>";
echo '<div class="product_price">$'. $price .'</div>';
echo "<form method=\"POST\" action=\"cart.php?action=add&icode=$item_code&iname=$item_name&iprice=$price&ilocation=$location\">";
echo "<input type=\"submit\" name=\"addtocart\" value=\"Add To Cart\"></form>";
echo "</td>";
if(($counter % $cols) == 0 ){
echo "</tr>";
}
$counter++;
}
if($nbsp > 0) { // Add unused column in last row
for ($i = 0; $i < $nbsp; $i++) {
echo '<td> </td>';
}
echo '</tr>';
}
}
}
echo '</table></div><div class="cleaner"></div>';
include('core/footer.php');
?>
well, brother, it's a bad approach how you doing it. But for learning, its fine. For + sign issue, you can use php urldecode, example:
<?php
$tosearch = 'a+b';
echo urldecode($tosearch);
it has its own pro/con thing but on high-level it will work for you, you can dig more into it if you like.
I want to Create a Shoping Cart on PHp,
The Code is simple, When the customer fill the QTY and click button Add to cart, the code will save the PRoduct ID and Qty to a Cart Table. But the problem is that Form inside the Looping. And How Can I Get ID and Qty only from The button that Customer Click.
The Program look like this
And The Script Like This
<?php
if(isset($_POST[ADD]))
{
$qty = $_POST[QTY];
$harga = $_POST[HARGA_ASLI];
$id = $_POST[ID];
print_r($_POST);
}
$kolom = 3;
$sql = "SELECT *,FORMAT(harga,0)AS harga_digit FROM item";
$hasil = mysql_query($sql);
echo "<form method=POST action=index.php>";
echo "<table>
<tr>";
$i = 0;
while($data=mysql_fetch_array($hasil))
{
if($i >= $kolom)
{
echo "</tr><tr>";
$i = 0;
}
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>
Rp. $data[harga_digit]<br>
<input type='submit' name='ADD' id='ADD' value='Add to Cart'>
<input type='text' name='QTY' id='QTY' placeholder='Qty' /><br>
<input type='hidden' name='HARGA_ASLI' id='HARGA_ASLI' value='$data[harga]' /><br>
<input type='hidden' name='ID' id='ID' value='$data[ID]' />
<br></td>";
}//end of while
echo "<tr></table>";
echo "</form>";
?>
If i fill the Qty and click Add to Cart, only the last Item can Post The Data.
How To Post The data Only for Customer Choose?
Im very Appreciated Your Answer.
Thanks
First, let's convert your MySQL to MySQLi. More explanation inside the comments /* */:
<?php
$connection=mysqli_connect("YourHost","YourUsername","YourPassword","NameofYourDatabase");
if(mysqli_connect_errno()){
echo "Error".mysqli_connect_error();
}
$res=mysqli_query($con,"SELECT * FROM item");
while($row=mysqli_fetch_array($res)){
$nameofsubmitbutton=$row['ID'];
if(isset($_POST[$nameofsubmitbutton])){
$nameofproduct=$row['namaproduk'];
$nameofnumbersubmitted=$nameofsubmitbutton."number";
$quantity=$_POST[$nameofnumbersubmitted];
if(empty($quantity)){
echo "You wanted to buy a ".$nameofproduct."?<br>Type in a number so you can add it to your cart.";
}
else {
mysqli_query($connection,"INSERT INTO yourTable ('','') VALUES ('$quantity','$nameofproduct')");
echo "You bought ".$quantity." of ".$nameofproduct;
}
} /* END OF IF ISSET */
} /* END OF WHILE LOOP $RES */
$kolom = 3;
$hasil = mysqli_query($connection,"SELECT *,FORMAT(harga,0) AS harga_digit FROM item"); /* YOU SURE WITH THIS QUERY? */
echo "<form method=POST action=''>"; /* SUBMIT ON ITSELF */
echo "<table><tr>";
$i = 0; /* THIS WOULD ALSO SET AS YOUR COUNTER */
while($data=mysqli_fetch_array($hasil))
{
$id=$data['ID'];
if($i >= $kolom){
echo "</tr><tr>";
$i = 0;
} /* END OF IF $i >= $KOLOM */
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>".$data[nama_produk]."</a><br>Rp. ".$data[harga_digit]."<br>"; /* IF TO ECHO VARIABLES, USE ".$variable." */
$numbername=$id."number";
echo "<input type='number' name='$numbername' id='QTY' placeholder='Qty' /><br>"; /* CHANGE YOUR INPUT TYPE TO NUMBER */
/* NO NEED FOR THE HIDDEN INPUT */
echo "<input type='submit' name='$id' id='ADD' value='Add to Cart'></td>"; /* CHANGE THE NAME OF SUBMIT BUTTON TO THE CORRESPONDING ID FROM YOUR TABLE */
} /* END OF WHILE LOOP */
echo "<tr></table>";
echo "</form>";
?>
I tried it on my local computer. You should too.
Here's a sample screen shot.
This may do the trick:
<?php
$kolom = 3;
$sql = "SELECT *,FORMAT(harga,0)AS harga_digit FROM item";
$hasil = mysql_query($sql);
while($data=mysql_fetch_array($hasil))
{
if(isset($_POST['ADD'.$data[ID]]))
{
$qty = $_POST['QTY'.$data[ID]];
$harga = $_POST['HARGA_ASLI'.$data[ID]];
$id = $_POST['ID'.$data[ID]];
print_r($_POST);
}
}
echo "<form method=POST action=index.php>";
echo "<table>
<tr>";
$i = 0;
while($data=mysql_fetch_array($hasil))
{
if($i >= $kolom)
{
echo "</tr><tr>";
$i = 0;
}
$i++;
echo "<td align='center'><br><a href='detailBarang.php?ID=$data[ID]'><img src='$data[img]' width='200' height='150'/><br>$data[nama_produk]</a><br>
Rp. $data[harga_digit]<br>
<input type='submit' name='ADD'".$data[ID]." id='ADD' value='Add to Cart'>
<input type='text' name='QTY'".$data[ID]." id='QTY' placeholder='Qty' /><br>
<input type='hidden' name='HARGA_ASLI'".$data[ID]." id='HARGA_ASLI' value='$data[harga]' /><br>
<input type='hidden' name='ID' id='ID'".$data[ID]." value='$data[ID]' />
<br></td>";
}//end of while
echo "<tr></table>";
echo "</form>";
?>
I am building this function which echo-s all table fields and has a checkbox along with it.
I need to print this fields after submission, i have this code, and no i haven't started something, not because i don't want but because i don't have any idea how to.. so i'm asking your help.
I need to print information in report.php please help.
<form action='report.php' method='post'>
<?php // Script 12.7 - sopping.php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM hostess");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);
// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $field); // instead of $i
echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . '][]" value="1"/> ' . $field_name . '</label></th>';
}
echo '</tr></thead>';
echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
$data = mysql_fetch_assoc($result);
echo '<tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $field);
if (isset($_POST['checkbox'][$field_name])) {
echo '<td>' . $data[$field_name] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
?>
<input type='submit' value='Submit' />
</form>
The report.php
<?php // Script 12.7 - sopping.php
$db = mysql_connect('localhost', 'root', '');
mysql_select_db('db_up', $db);
foreach($_POST['checkbox'] as $key => $value)
?>
Try this one:
if(isset($_POST['Submit'])) {
echo "<pre>";
$checked = implode(',', $_POST['checkbox']);
echo $checked;
}
I want to select particular fields from a database using a checkbox in PHP.
My code is:
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numfields = mysql_num_fields($result);
echo "<table>\n<tr>";
for ($i=0; $i < $numfields; $i++) // Header
{
echo '<th>'.mysql_field_name($result, $i).'</th>';
echo '<tr><td><input type="hidden" name="checkbox[]" value="false"/></td></tr>';
echo '<tr><td><input type="checkbox" name="checkbox[]" value="true"/></td></tr>';
$checkbox[$i] = isset($_POST['checkbox'][$i]) ? true : false;
if(isset($checkbox))
{
foreach($checkbox as $value)
{
echo $value."<br>"; //it will print the value of your checkbox that you checked
}
}
}
I think something like this will give you better results.
<?php
echo "<table border='1' class='tabtext'>";
$result = mysql_query("SELECT * FROM orders");
$numrows = mysql_num_rows($result);
$numfields = mysql_num_fields($result);
// show headers
echo '<thead><tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
echo '<th><label><input type="checkbox" name="checkbox[' . $field_name . ']" value="1"/> ' . $field_name . '</label></th>';
}
echo '</tr></thead>';
echo '<tbody>';
for ($row = 0; $row < $numrows; $row++) {
$data = mysql_fetch_assoc($result);
echo '<tr>';
for ($field = 0; $field < $numfields; $field++) {
$field_name = mysql_field_name($result, $i);
if (isset($_POST['checkbox'][$field_name])) {
echo '<td>' . $data[$field_name] . '</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';