How to remove repeating headings - php

spent hours unsuccessfully trying to figure this out, just not a good coder. I have 2 headings called title and date and I'm trying to stop them from repeating before every row. I just want the heading once at the top. What do I have to change to accomplish this. Thanks!!!! Use bitcoin to search.
<thead>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='5%'>Date</th>
</tr>
</thead>
<?php
define("ROW_PER_PAGE",15);
require_once('db.php');
?>
<html>
<head>
<style>
body{width:615px;font-family:arial;letter-spacing:1px;line-height:20px;}
.tbl-qa{width: 100%;font-size:0.9em;background-color: #f5f5f5;}
.tbl-qa th.table-header {padding: 5px;text-align: left;padding:10px;}
.tbl-qa .table-row td {padding:10px;background-color: #FDFDFD;vertical-align:top;}
.button_link {color:#FFF;text-decoration:none; background-color:#428a8e;padding:10px;}
#keyword{border: #CCC 1px solid; border-radius: 4px; padding: 7px;background:url("demo-search-icon.png") no-repeat center right 7px;}
.btn-page{margin-right:10px;padding:5px 10px; border: #CCC 1px solid; background:#FFF; border-radius:4px;cursor:pointer;}
.btn-page:hover{background:#F0F0F0;}
.btn-page.current{background:#F0F0F0;}
</style>
</head>
<body>
<?php
$search_keyword = '';
if(!empty($_POST['search']['keyword'])) {
$search_keyword = $_POST['search']['keyword'];
$sql = 'SELECT * FROM posts WHERE post_title LIKE :keyword OR description LIKE :keyword OR post_at LIKE :keyword ORDER BY id DESC ';
/* Pagination Code starts */
$per_page_html = '';
$page = 1;
$start=0;
if(!empty($_POST["page"])) {
$page = $_POST["page"];
$start=($page-1) * ROW_PER_PAGE;
}
$limit=" limit " . $start . "," . ROW_PER_PAGE;
$pagination_statement = $pdo_conn->prepare($sql);
$pagination_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pagination_statement->execute();
$row_count = $pagination_statement->rowCount();
if(!empty($row_count)){
$per_page_html .= "<div style='text-align:center;margin:20px 0px;'>";
$page_count=ceil($row_count/ROW_PER_PAGE);
if($page_count>1) {
for($i=1;$i<=$page_count;$i++){
if($i==$page){
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page current" />';
} else {
$per_page_html .= '<input type="submit" name="page" value="' . $i . '" class="btn-page" />';
}
}
}
$per_page_html .= "</div>";
}
$query = $sql.$limit;
$pdo_statement = $pdo_conn->prepare($query);
$pdo_statement->bindValue(':keyword', '%' . $search_keyword . '%', PDO::PARAM_STR);
$pdo_statement->execute();
$result = $pdo_statement->fetchAll();
}
?>
<form name='frmSearch' action='' method='post'>
<div style='text-align:right;margin:20px 0px;'><input type='text' name='search[keyword]' value="<?php echo $search_keyword; ?>" id='keyword' maxlength='25'></div>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<table class='tbl-qa'>
<thead>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='5%'>Date</th>
</tr>
</thead>
<tbody id='table-body'>
<tr class='table-row'>
<td><a style="text-decoration: none;" href="<?php echo $row['description']; ?>">
<?php echo $row['post_title']; ?></a></td>
<td><?php echo $row['post_at']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
<?php echo $per_page_html; ?>
</form>
</body>
</html>

This way it will works:
Array to test (use your $result in your case)
$result=array(
array('post_title'=>'title 1','post_at'=>'at 1'),
array('post_title'=>'title 2','post_at'=>'at 2'),
array('post_title'=>'title 3','post_at'=>'at 3'),
)
Html:
<table class='tbl-qa'>
<tr>
<th class='table-header' width='20%'>Title</th>
<th class='table-header' width='5%'>Date</th>
</tr>
<?php
if(!empty($result)) {
foreach($result as $row) {
?>
<tr class='table-row'>
<td><a style="text-decoration: none;" href="<?php echo $row['description']; ?>">
<?php echo $row['post_title']; ?></a></td>
<td><?php echo $row['post_at']; ?></td>
</tr>
<?php
}
?>
<?php
}
?>
As you can see, the header if written just once because it is out of the loop whilst the row inside the loop will be repeated.
Finally close the table out of the loop.

Related

$_SESSION problem - I have same ID on all <tr>

I don’t know, how I do this: I want to add $id from database to $_SESSION["dbID"] and after click, it shows me more information from database. But table generates in while function and $_SESSION["dbID"] every time set to the highest number of row from table. Please, Can you anyone change my code as I have $_SESSION["dbID"] on every <tr> of table different? Thank you
while($row = $result->fetch_assoc())
{
$id=$row['ID'];
$name=$row['Name'];
$subject=$row['Subject'];
$date=$row['Date'];
echo
'<tr class="trX" id="'.$id.'" href="google.com&id='.$row['ID'].'">
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $id . '</a></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $name . '</td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $subject . '</td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $date . '</td>
</tr>';
}
$_SESSION["dbID"] = $id;
echo ' </table> ';
Explanation
You can do away with all of the a tags and use JavaScript to handle the redirect...
$url = "/path/to/file.php?id=" . $id;
Set the URL to the page that you want to link to. The line above shows the link to the file "file.php" on the server with the query string "id=$id".
onclick="window.location.href='...'"
The line above is a JS equivalent of href. If you want to navigate to a server outside of your domain remember to add the full url e.g. https://www.website.com
Code example
while ($row = $result->fetch_assoc()) {
$id = $row['ID'];
$name = $row['Name'];
$subject = $row['Subject'];
$date = $row['Date'];
$url = "/url/path.php?id=" . $id;
echo <<<EOT
<tr class="trX" onclick="window.location.href='{$url}'">
<td class="tdX">{$id}</td>
<td class="tdX">{$name}</td>
<td class="tdX">{$subject}</td>
<td class="tdX">{$date}</td>
</tr>
EOT;
}
echo ' </table> ';
if (mysqli_num_rows($sql) > 0) {
$row = mysqli_fetch_assoc($sql);
}
while($row = $result->fetch_assoc()){ ?>
<tr class="trX" id="'.<?php echo $row['ID']; ?>.'" href="google.com&id='.$row['ID'].'">
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['ID']; ?></a></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Name']; ?></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Subject']; ?></td>
<td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Date']; ?> </td>
</tr>
<?php } ?>
Dont define the rows just make a call for them in the databse.

Session variables from for not stored and accessible on other pages

I hope I'm not asking a question already answered, I have searched extensively on the site for an answer to my problem but can not manage to find a solution.
I have the following problem.
I have a form in which is a shopping cart, it is supposed to apply a coupon and then save those values so they can be used later on different pages.
For some reason however, these session variables are not saved.
My form looks like this:
<?php
session_start();
$cart_custom = json_decode($_COOKIE['view_cart_custom_fab'],true);
//print_r($cart_custom);
include_once("inc/header.php");
//set_add_to_cart_cookie();
$fetch_user_info = mysqli_query($conn,"SELECT * FROM `st_customer` WHERE `email`= '{$_SESSION['USER_EMAIL']}'");
$fetch_info = mysqli_fetch_array($fetch_user_info);
$size_id = ($_REQUEST['id'] ? $_REQUEST['id'] : $_COOKIE['fabric_size_id']);
$fabric_id = $_COOKIE['fabric_id'];
$fabric_back = $_COOKIE['fabric_back'];
$shirts = $_COOKIE['shirts'];
$fabric_arms = $_COOKIE['fabric_arms'];
$fabric_pocket = $_COOKIE['fabric_pocket'];
$fabric_cuff = $_COOKIE['fabric_cuff'];
$fabric_boy_border = $_COOKIE['fabric_boy_border'];
$fabric = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_id'");
$fabric_record = mysqli_fetch_array($fabric);
$fabric_back_data = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_back'");
$fabric_back_record = mysqli_fetch_array($fabric_back_data);
$arms = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_arms'");
$arms_record = mysqli_fetch_array($arms);
$pocket = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_pocket'");
$pocket_record = mysqli_fetch_array($pocket);
$cuff = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_cuff'");
$cuff_record = mysqli_fetch_array($cuff);
$border = mysqli_query($conn,"SELECT * FROM `st-fabrics` where `id` = '$fabric_boy_border'");
$border_record = mysqli_fetch_array($border);
$select_size = mysqli_query($conn,"SELECT * FROM `st_sizes` where `size_id` = '$size_id'");
$user_select_size = mysqli_fetch_array($select_size);
$model_id = $user_select_size['size_model'];
$model = mysqli_query($conn,"SELECT * FROM `st_model` where `model_id` = '$model_id'");
$model_name = mysqli_fetch_array($model);
if($shirts=="half_shirt"){
$shirt = $user_select_size['half_sleeve'];
}
if($shirts=="full_tshirt"){
$shirt = $user_select_size['full_sleeve'];
}
$cm1 = $fabric_record['fabric_price']/10;
$cm2 = $arms_record['fabric_price']/10;
$cm3 = $fabric_back_record['fabric_price']/10;
$front_size = ($cm1*$user_select_size['front']*100);
$back_size = ($cm3*$user_select_size['size_back']*100);
$arms_size = ($cm2*$shirt*100);
$front_back_total = $user_select_size['front']+$user_select_size['size_back'];
$custom_total = ($front_size+$back_size+$arms_size);
if(isset($_POST['apply_coupon_btn'])){
$query_coupon = mysqli_query($conn,"SELECT * FROM `st_coupon` WHERE `coupon_code`='{$_POST['coupon_apply']}' AND `status`='1'");
$fetch_coupon = mysqli_fetch_array($query_coupon);
$message = "tmp2";
if(isset($fetch_coupon)){
$_SESSION['coupon_data'] = $fetch_coupon;
$check_user_id = mysqli_query($conn,"SELECT * FROM `st_order` as so JOIN `st_coupon` as sc on so.`coupon_id`=sc.`coupon_id` WHERE sc.`coupon_code`='{$_POST['coupon_apply']}' AND so.`user_id`={$_SESSION['user_id']}");
$message = "tmp3";
if(mysqli_num_rows($check_user_id)){
$message = "U hebt deze kortingscode reeds gebruikt";
}else{
if($fetch_coupon['coupon_type'] == "price"){
$_SESSION['price_coupon_price'] = $fetch_coupon['coupon_value'];
unset($_SESSION['price_coupon_percent']);
}elseif($fetch_coupon['coupon_type'] == "percent"){
$_SESSION['price_coupon_percent'] = $fetch_coupon['coupon_value'];
unset($_SESSION['price_coupon_price']);
}
}
}else{
$message = "Foute kortingscode";
}
}
if(isset($_POST['remove_coupon_btn'])){
unset($_SESSION['price_coupon_percent']);
unset($_SESSION['price_coupon_price']);
unset($_SESSION['coupon_data']);
unset($_SESSION['price_coupon_value']);
header("location: winkelwagen.php");
}
if(isset($_POST['update_cart']))
{
echo "update cart";
foreach($_POST['custom_qty_cart'] as $key=>$qtys){
$cart_custom[$key]['price']['qty'] = $qtys;
}
setcookie('view_cart_custom_fab', json_encode($cart_custom), time() + (86400 * 2), "/");
setcookie('custom_qty', $_POST['custom_qty'], time() + (86400 * 2), "/");
$custom_total = $custom_total*$_POST['custom_qty'];
setcookie('custom', $custom_total, time() + (86400 * 2), "/");
foreach($_POST["qty"] as $key => $qty) { //Print each item, quantity and price.
$id = $_POST["product_id"][$key];
$_SESSION["products"][$id]["fabric_qty"] = $qty;
}
header("location: winkelwagen.php");
}
if(isset($_POST['checkout_btn'])){
if($_SESSION['USER_EMAIL']){
header("location: afrekenen.php");
}else{
header("location: winkelwagen.php#login-modelbox");
}
}
?>
<div class="container">
<h4><?php echo $_SESSION['price_coupon_value']; ?></h4>
<h4><?php echo $_SESSION['coupon_data']; ?></h4>
<div class="main_content fabric-content margin_top">
<?php
?>
<?php if (!empty($_SESSION["products"]) || count($cart_custom) > 0) { ?>
<form method="POST" action="winkelwagen.php" style="width:60%; float:left; position:relative; left: 50%;">
<table class="margin_top" style="float: left; position: relative; left: -50%;">
<tr>
<th data-field="id" colspan="1" style="width: 15%;"></th>
<th data-field="id" colspan="1" style="width: 35%;">Naam</th>
<th data-field="id" colspan="1" style="width: 35%;">Aantal</th>
<th data-field="id" colspan="1" style="width: 15%;">Prijs</th>
</tr>
<?php if(count($cart_custom) > 0){ ?>
<?php
$total_price = 0;
foreach($cart_custom as $key=>$val){
$cookie_model = $cart_custom[$key]['model'];
$cookie_price = $cart_custom[$key]['price'];
$model_name = $cookie_model['model'];
if($model_name == "wranglan_model"){
$model_img = "image/t-shirt.png";
}elseif($model_name == "round_model"){
$model_img = "image/t-shirt.png";
}
$model_qty = str_replace(",",".",$cookie_price['qty']);
$model_price = $model_qty * $cookie_price['ttl_price'];
$total_price = $model_qty * $cookie_price['ttl_price'] + $total_price;
if(isset($_GET['remove_id'])){
unset($cart_custom[$_GET['remove_id']]);
setcookie('view_cart_custom_fab', json_encode($cart_custom), time() + (86400 * 2), "/");
header("location: winkelwagen.php");
}
?>
<tr>
<td colspan='1' style="width: 15%;">
<img src='<?php echo $model_img; ?>' alt='' class='img_table responsive_img' />
</td>
<td colspan='1' style="width: 35%;">
T-shirt op smaak
</td>
<td colspan='1' style="width: 35%;">
<input id='update_cart' name='update_cart' type='hidden' value='' />
<input class="qty" type="text" name="custom_qty_cart[<?php echo $key; ?>]" value="<?php echo $model_qty; ?>" style="width: 20%; text-align:right;"/>
<input name='Update' style="width: 18px; vertical-align:bottom" type='image' value='update_cart' src='/image/update.jpg' alt='Update' onclick = "return setHidden('update_cart');" />
<img src="/image/recycle.jpg" style="width: 18px; vertical-align:bottom">
</td>
<td colspan='1' style="width: 15%;">
<?php echo price_format($model_price); ?>
</td>
</tr>
<?php
}
?>
<?php } ?>
<?php if(!empty($_SESSION["products"])){?>
<?php
if (isset($_SESSION["products"]) && count($_SESSION["products"]) > 0) {
$total = 0;
$list_tax = '';
foreach ($_SESSION["products"] as $product) { //Print each item, quantity and price.
//print_r($_SESSION["products"]);
$product_name = $product["fabric_name"];
$product_id = $product["id"];
//$product_qty = ($product["fabric_qty"] > 1 ? $product["fabric_qty"]:1);
$product_qty = str_replace(",",".",$product["fabric_qty"]);
//$product_price = ($product_qty>1 ? $product["fabric_price"] * $product_qty : $product["fabric_price"]);
$product_price = ($product["fabric_price"] * $product_qty);
$product_code = $product["product_code"];
$product_color = $product["product_color"];
$product_size = $product["product_size"];
$item_price = $product_price;
$total_cost_price = $total_cost_price + $item_price;
//$item_price = sprintf("%01.2f",($product_price * $product_qty)); // price x qty = total item price
if(isset($_GET['removeid'])){
unset($_SESSION["products"][$_GET['removeid']]);
header("location: winkelwagen.php");
}
$cart_box .=
"<tr><td colspan='1' style='width: 15%'><img src='admin/images/fabric/" . $product['fabric_texture'] . "' alt='' class='img_table responsive_img' /></td>
<td colspan='1' style='width: 35%'>$product_name</td>
<td colspan='1' style='width: 35%'>
<input id='update_cart' name='update_cart' type='hidden' value='' />
<input type='text' name='qty[]' class='qty' min='1' max='100' value='".$product_qty."' style='width: 20%; text-align:right;'/>
<input name='Update' style='width: 18px; vertical-align:bottom' type='image' value='update_cart' src='/image/update.jpg' alt='Update' onclick = 'return setHidden('update_cart');' />
<a href='?removeid=$product_id' style='width: 18px; vertical-align:bottom'><img src='/image/recycle.jpg' style='width: 18px; vertical-align:bottom'></a>
<input type='hidden' name='product_id[]' value='".$product_id."' /></td>
<td colspan='1' style='width: 15%'>".price_format($item_price)."</td>";
$subtotal = ($product_price * $product_qty); //Multiply item quantity * price
$total = ($total + $subtotal); //Add up to total price
}
$grand_total = $total + $shipping_cost; //grand total
foreach ($taxes as $key => $value) { //list and calculate all taxes in array
$tax_amount = round($total * ($value / 100));
$tax_item[$key] = $tax_amount;
$grand_total = $grand_total + $tax_amount;
}
foreach ($tax_item as $key => $value) { //taxes List
$list_tax .= $key . ' ' . $currency . sprintf("%01.2f", $value) . '<br />';
}
$shipping_cost = ($shipping_cost) ? 'Shipping Cost : ' . $currency . sprintf("%01.2f", $shipping_cost) . '<br />' : '';
echo $cart_box;
}
?>
<?php } ?>
<?php
$fetch_shipping_price = mysqli_query($conn,"SELECT * FROM `st_custom_price` WHERE `meta_key`='shipping'");
$fetch_price = mysqli_fetch_array($fetch_shipping_price);
$shipping_price = $fetch_price['meta_price'];
$product_total = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']);
if($_SESSION['price_coupon_price']){
$total_amount = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']) - $_SESSION['price_coupon_price'];
$_SESSION['price_coupon_value'] = $_SESSION['price_coupon_price'];
}elseif($_SESSION['price_coupon_percent']){
$total_amount = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']);
$percent_total = ($_SESSION['price_coupon_percent'] / 100) * $total_amount;
$_SESSION['price_coupon_value'] = $percent_total;
$total_amount = $total_amount - $percent_total;
}else{
$total_amount = $total_price + $total_cost_price + ($price_detail['ttl_price']*$price_detail['qty']);
}
$ttl_ship = $total_amount + $shipping_price;
setcookie('total_cost', $ttl_ship, time() + (86400 * 2), "/");
setcookie('total_cost', $ttl_ship, time() + (86400 * 2), "/");
?>
<?php if(!empty($total_amount) || count($cart_custom) > 0){ ?>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">Totaal</th>
<th data-field="id" colspan="1" style="width: 15%;"> <?php echo price_format($product_total); ?></th>
</tr>
<?php if($_SESSION['price_coupon_price'] || $_SESSION['price_coupon_percent']){
$discount_type = ($_SESSION['coupon_data']['coupon_type'] == "price" ? " €" : " percent ");
?>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">
<input id='remove_coupon_btn' name='remove_coupon_btn' type='hidden' value='' />
Kortingscode <?php echo $_SESSION['coupon_data']['coupon_name']; ?><input name='remove_coupon_btn' style="width: 18px; vertical-align:bottom" type='image' value='remove_coupon_btn' src='/image/recycle.jpg' alt='Remove coupon' onclick = "return setHidden('remove_coupon_btn');" />
</th>
<th style="width: 15%;"><?php echo price_format($total_amount-$product_total); ?></th>
</tr>
<?php } ?>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">Verzendingskosten</th>
<th data-field="id" colspan="1" style="width: 15%;"> <?php echo price_format($shipping_price); ?></th>
</tr>
<tr>
<th data-field="id" colspan="3" style="width: 85%;">Te betalen</th>
<th data-field="id" colspan="1" style="width: 15%;"> <?php echo price_format($ttl_ship); ?></th>
</tr>
</table>
<?php echo $message; ?>
<div class="more-products-pagination" style="float: left; position: relative; left: -50%;">
<div class="coupon_row" style="margin:5px 20px">
<?php if($_SESSION['price_coupon_price'] || $_SESSION['price_coupon_percent']){ ?>
<?php } else { ?>
<ul>
<li>Kortingscode</li>
<li><input type="text" name="coupon_apply" value=""><input type="submit" name="apply_coupon_btn" value="Toepassen" class="add_to_cart_btn" style="border: 1px solid #fff; display: inline-block;float: right;"></li>
</ul>
<?php } ?>
</div>
<div class="pagination_btn pagination_btn_right">
<input type="submit" class="add_to_cart_btn" value="Afrekenen" name="checkout_btn">
</div>
</div>
<?php } ?>
</form>
<?php }else {
echo "Geen producten in uw winkelwagen.";
}?>
</div>
</div>
<script language="javascript">
function setHidden(whichButton)
{
var element = document.getElementById(whichButton);
element.value = whichButton;
return true;
}
</script>
<?php
include_once("inc/footer.php");
?>
<style>
table {
border-collapse: collapse;
width: 100%;
background: rgba(253, 245, 208,0.6);
box-shadow: 0px 0px 2px #C1C1C1;
}
th, td {
text-align: left;
padding: 8px;
color:#444;
}
td{
}
th {
background-color: #BD3F6F;
color: white;
font-weight: 400;
font-size: 16px;
}
</style>
When I then try to access the variable $_SESSION['coupon_data'] on a different page this does not work, it is empty.
I set the variable here in this code:
if(isset($_POST['apply_coupon_btn'])){
$query_coupon = mysqli_query($conn,"SELECT * FROM `st_coupon` WHERE `coupon_code`='{$_POST['coupon_apply']}' AND `status`='1'");
$fetch_coupon = mysqli_fetch_array($query_coupon);
$message = "tmp2";
if(isset($fetch_coupon)){
$_SESSION['coupon_data'] = $fetch_coupon;
..
}
And then I call the next page here:
if(isset($_POST['checkout_btn'])){
if($_SESSION['USER_EMAIL']){
header("location: afrekenen.php");
}else{
header("location: winkelwagen.php#login-modelbox");
}
}
Even if I call the update of the cart items in this same page, the session variables disapear for some reason which I do not understand:
if(isset($_POST['update_cart']))
{
echo "update cart";
foreach($_POST['custom_qty_cart'] as $key=>$qtys){
$cart_custom[$key]['price']['qty'] = $qtys;
}
setcookie('view_cart_custom_fab', json_encode($cart_custom), time() + (86400 * 2), "/");
setcookie('custom_qty', $_POST['custom_qty'], time() + (86400 * 2), "/");
$custom_total = $custom_total*$_POST['custom_qty'];
setcookie('custom', $custom_total, time() + (86400 * 2), "/");
foreach($_POST["qty"] as $key => $qty) { //Print each item, quantity and price.
$id = $_POST["product_id"][$key];
$_SESSION["products"][$id]["fabric_qty"] = $qty;
}
header("location: winkelwagen.php");
}
I am utterly baffled and hope someone can help me out.
issue resolved, turns out there was another call to session_start in one of the includes which was creating a separate session and hence not allowing the correct globalisation.

Next and Previous Buttons with php

I have been having a problem getting next and previous buttons to work using a php webpage connected to mysql database. I am not sure what is wrong but i think it may have something to do with the offset. Right now the button are not working at all you click them and nothing happens. Also the form is made as a search page.
Here is the search code:
<?php
include("../web-admin/dbconfiginc.php");
$result = mysql_query("SELECT * FROM videos WHERE ID = '$ID'");
$row = mysql_fetch_array($result);
?><strong></strong>
<title>Search </title>
</head>
<body>
<table align="center" width="1024" border="0">
<tr>
<td valign="top" align="center" width="70%" style="padding-top:10px;" class="lhc">
<div align="center" style="margin-top: 50px; padding-bottom: 50px;">
<?php
$Keyword = $_POST['Keyword'];
$GuestName = $_POST['GuestName'];
$Day = $_POST['Day'];
$Month = $_POST['Month'];
$Year = $_POST['Year'];
if(isset($cancel))
{
header("Location:index.php");
exit;
}
$qry_string = "SELECT * FROM videos ";
$search = "";
if(!empty($Keyword))
{
$End_String = "(Blurb LIKE '%$Keyword%' OR Title LIKE '%$Keyword%')";
$search .="&Keyword=$Keyword";
}
if(!empty($GuestName))
{
if(isset($End_String))
{
$End_String .= " AND (GuestName LIKE '%$GuestName%')";
}
else
{
$End_String = "(GuestName LIKE '%$GuestName%')";
}
$search .="&GuestName=$GuestName";
}
if(!empty($Day))
{
if(isset($End_String))
{
$End_String .= " AND (DAYOFMONTH(Publish_Date) = '$Day')";
}
else
{
$End_String = "(DAYOFMONTH(Publish_Date) = '$Day')";
}
$search .="&Day=$Day";
}
if(!empty($Month))
{
if(isset($End_String))
{
$End_String .= " AND (MONTH(Publish_Date) = '$Month')";
}
else
{
$End_String = "(MONTH(Publish_Date) = '$Month')";
}
$search .="&Month=$Month";
}
if(!empty($Year))
{
if(isset($End_String))
{
$End_String .= " AND (YEAR(Publish_Date) = '$Year')";
}
else
{
$End_String = "(YEAR(Publish_Date) = '$Year')";
}
$search .="&Year=$Year";
}
if(!empty($Active))
{
if(isset($End_String))
{
$End_String .= " AND (GuestName LIKE '%$GuestName%')";
}
else
{
$End_String = "(GuestName LIKE '%$GuestName%')";
}
$search .="&GuestName=$GuestName";
}
if (!isset($offset)) $offset=0;
if(isset($End_String))
{
$qry_string = $qry_string." WHERE ".$End_String . "ORDER BY Publish_Date DESC LIMIT $offset, 101";
}
else
{
$qry_string = $qry_string."ORDER BY Publish_Date DESC LIMIT $offset, 101";
}
$result = mysql_query($qry_string);
echo mysql_error();
?>
And here is the code that displays the results from the database
<?php
$num_records = mysql_num_rows($result);
if (!isset($search))
$search = "";
?>
<table width="100%" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0"><tr><td>
</td></tr></table>
<table width="95%" align="center" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<tr>
<td width=25%>
<?php
if ($offset > 99) {
echo '<a href='.$_SERVER['PHP_SELF'].'?$offset='.($offset-100).'>Previous 100</a>';
} else {
echo 'Previous 100';
}
?>
</td>
<td align=center width=10%>
<span class="content1"><a href=search.php>Search </a></span>
</td>
<td align=right width=25%>
<?php
if($num_records == 101) {
echo 'Next 100';
} else {
echo 'Next 100';
}
?>
</td>
</tr>
</table>
<table align="center" border="0" cellpadding="3" cellspacing="1" bgcolor="#666666" style="margin: 5px 15px; 5px 10px;">
<tr style="background: #9ae6f1; font-family: Verdana; font-weight: bold; font-size: 18px;">
<td style="width: 65%; padding: 5px;">
Video
</td>
<td align="center" style="width: 10%; padding: 5px;">
Guest Name
</td>
<td align="center" style="width: 10%; padding: 5px;">
Date
</td>
</tr>
<?php
$count = 1;
$bgc = 1;
while($row = mysql_fetch_array($result))
{
if ($count > 100) break;
echo '<tr style="background: ';
if (($bgc == 0 ? $bgc=1 : $bgc=0) == 0) echo "#FFFFFF";
else echo "#E6E6E6";
echo ';">';
echo '<td><div style="padding: 3px;" class="content"><a href=../../watch/watch.php?ID='.$row[ID].'>'.$row[Title].'</a></div></td>';
echo '<td><div style="padding: 3px;" class="content">'.$row[GuestName].'</div></td>';
echo '<td><div align="center" style="padding: 3px;" class="content">'.$row[Publish_Date].'</div></td></tr>';
$count++;
}
?>
</table>
<table width="95%" align="center" style="border-collapse: collapse" bordercolor="#111111" cellpadding="0" cellspacing="0">
<tr>
<td width=25%>
<?php
if($offset > 99) {
echo '<span class="content"><a href='.$_SERVER['PHP_SELF'].'?offset='.($offset-100).$search.'>Previous 100</a></span>';
} else {
echo 'Previous 100';
}
?>
</td>
<td align=center width=10%>
<span class="content1"><a href=search.php>Search </a></span>
</td>
<td align=right width=25%>
<?php
if($num_records == 101) {
echo '<span class="content"><a href='.$_SERVER['PHP_SELF'].'?offset='.($offset+100).$search.'>Next 100</a></span>';
} else {
echo 'Next 100';
}
?>
</td>
</tr>
</table>
Would really appreciate some help trying to figure this out or some points in the right direction. Thank you!

Viewing queried data from mysql database in a table

I am attempting to update the code for my web page's search function, right now it is not returning anything. I have been working with it for a little while and not getting anything out of it.
This is the HTML search code:
<form method="post" action="words_results1.php">
<table align="center">
<tr>
<td>Keyword</td>
<td><input type="text" name="Keyword" /></td>
</tr>
<tr>
<td>Author</td>
<td><input type="text" name="Author" /></td>
</tr>
<tr>
<td valign=bottom>Words Posted<BR />on or before</td>
<td valign=top>
<table>
<tr>
<td width="33%">Day</td>
<td width="33%">Month</td>
<td width="34%">Year</td>
</tr>
<tr>
<td>
<select name=Day>
<?php
echo '<option></option>';
for($count = 1; $count <= 31; ++$count)
{
echo "<option>$count</option>";
}
?>
</select>
</td>
<td>
<select name=Month>
<?php
echo '<option></option>';
for($count = 1; $count <= 12; $count++)
{
echo "<option value=$count>".date("M", mktime(0,0,0,$count,1, 2000))."</option>";
}
?>
</select>
</td>
<td>
<select name=Year>
<?php
echo '<option></option>';
for($count = date("Y"); $count >= 1997; $count--)
{
echo "<option>$count</option>";
}
?>
</select>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<BR />
<input type="submit" value="Search" />
<input type="submit" name="cancel" value="Cancel" />
</td>
</tr>
</table>
</form>
PHP
<?php
if(isset($_POST['cancel']))
{
echo("index.html");
exit;
}
$qry_string = "SELECT * FROM Words";
$search = "";
if(!empty($Keyword))
{
$End_String = "(Word LIKE '%$Keyword%' OR Title LIKE '%$Keyword%')";
$search .="&Keyword=$Keyword";
}
if(!empty($Author))
{
if(isset($End_String))
{
$End_String .= " AND (Author LIKE '%$Author%')";
}
else
{
$End_String = "(Author LIKE '%$Author%')";
}
$search .="&Author=$Author";
}
if(!empty($Day))
{
if(isset($End_String))
{
$End_String .= " AND (DAYOFMONTH(Date_Created) = '$Day')";
}
else
{
$End_String = "(DAYOFMONTH(Date_Created) = '$Day')";
}
$search .="&Day=$Day";
}
if(!empty($Month))
{
if(isset($End_String))
{
$End_String .= "AND (MONTH(Date_Created) = '$Month')";
}
else
{
$End_String = "(MONTH(Date_Created) = '$Month')";
}
$search .="&Month=$Month";
}
if(!empty($Year))
{
if(isset($End_String))
{
$End_String .= " AND (YEAR(Date_Created) = '$Year')";
}
else
{
$End_String = "(YEAR(Date_Created) = '$Year')";
}
$search .="&Year=$Year";
}
if (!isset($offset)) $offset=0;
if(isset($End_String))
{
$qry_string = $qry_string." WHERE ".$End_String . " ORDER BY Date_Created DESC LIMIT $offset,101";
}
else
{
$qry_string = $qry_string." ORDER BY Date_Created DESC LIMIT $offset,101";
}
// echo $qry_string . "<P><HR><P>";
$result = mysql_query($qry_string);
echo mysql_error();
?>
This last bit is the code that forms the table, I have an assumption that the problem lies here but honestly am not sure at this point
<table style="margin: 5px 15px; 5px 20px;" align="center" bgcolor="#666666" border="0" cellpadding="3" cellspacing="1">
<tbody><tr style="background: #04C1DE; font-family: Verdana; font-weight: bold; font-size: 18px;">
<td style="width: 50%; padding: 5px;">
Word
</td>
<td style="width: 20%; padding: 5px;">
Author
</td>
<td style="width: 10%; padding: 5px;">
Date
</td>
<td>Category</td>
<td>Active?</td>
<td> </td>
<td> </td>
</tr>
</tbody>
</tr>
<?php
$count = 1;
$bgc = 0;
while($row = mysql_fetch_array($sql))
{
if ($count > 100) break;
echo '<tr style="background: ';
if ($bgc==0) echo "#FFFFFF";
else echo "#CFEBFD";
$bgc == 0?$bgc=1:$bgc=0;
echo ';">';
echo "<td><a href=../../words/display_word.php?ID=$row[ID]>$row[Title]</a></td>";
echo "<td>$row[Author]</td><td>$row[Display_Date]</td><td>$row[category]</td>";
if($row[active])
{
echo "<td>YES</td>";
}
else
{
echo "<td>NO</td>";
}
echo "<td>$row[link_count]</td>";
if($row[Title] != "")
{
echo "<td><a href=words_edit.html?ID=$row[ID]>Edit</a></td></tr>";
}
else
{
echo "</tr>";
}
$count++;
}
?>
It seems,you are not collecting the value of
$Keyword=$_POST['Keyword'];
and add ,closing table tag to display the results in the table format correctly.

I need to insert data into mysql from a dynamic table with selects created with PHP

Wondering if someone could help me update mysql table with data from a couple of selects created dynamically with PHP, I have created the following code, but it seems not to be working, really appreciate your help:
`include_once('../includes/connection.php');`
// Query that retrieves events
$con = "SELECT * FROM evenement WHERE approved = 'no' ORDER BY id";
$result = mysqli_query($connection, $con);
if($con){
$registry = mysqli_affected_rows($connection);
if($registry > 0){
echo '
<h1 align="center">Events pending approval</h1>
<br><table width="100%" align="center" border="0" border-spacing="2px" cellspacing="1" cellpadding="1">
<form action="approveReject.php" method="post" >
<tr bgcolor="#3333FF" style="color:white; font-family:Tahoma, Geneva, sans-serif; font-size:15px"">
<th align="center"><strong>Title</strong></th>
<th align="center"><strong>Details</strong></th>
<th align="center"><strong>Category</strong></th>
<th align="center"><strong>Start</strong></th>
<th align="center"><strong>End</strong></th>
<th align="center"><strong>All Day event?</strong></th>
<th align="center"><strong>Approved</strong></th>
</tr>';
$color = "1";
while($registry = mysqli_fetch_array($result, MYSQLI_ASSOC)){
if($color==1){
echo '<tr bgcolor="#F8F8F8" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color="1";
}
echo '
<form action="" method="post">
<input type="hidden" value="'.$registry['id'].'" name="id[]" id="id">
<th div align="center">'.$registry['title'].'
<td div align="center">'.$registry['details'].'
<td div align="center">'.$registry['category'].'
<td div align="center">'.$registry['start'].'
<td div align="center">'.$registry['end'].'
<td div align="center"><select name="allDay[]" id="allDay">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<td div align="center"><select name="ap_re[]" id="ap_re">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</tr>';
}
?>
<td><input type="submit" name="button" id="button" value="Submit" style="height:1.8em; width:7.3em;" /></td>
<?php
echo '</form>
';
echo '
</form>
</table>';
}
}else{
echo '<h1>There are no new requests to be approved</h1>';
}
if(isset($_POST['Submit'])){
if($_POST['allDay'] == 'Yes'){
$allDay = 'true';
}else{
$allDay = 'false';
}
$ap_re = $_POST['ap_re'];
$i = 0;
foreach($_POST['id'] as $id){
$udpate_qry = "UPDATE evenement SET allDay='".$allDay."', approved='".$ap_re."', WHERE id='".$id."'";
$result_udpate_qry = mysqli_query($connection, $update_qry);
$i++;
}
}
?>
I don't think that I have solved all your issues, but this might help you further.
<?php
include_once('../includes/connection.php');
if (isset($_POST['submit'])) {
if ($_POST['allDay'] == 'Yes') {
$allDay = 'true';
} else {
$allDay = 'false';
}
$ap_re = $_POST['ap_re'];
$id = $_POST['id'];
$i = 0;
while ($id) {
$update_qry = "UPDATE evenement SET allDay='" . $allDay . "', approved='".$ap_re[$i]. "' WHERE id='".$id[$i]."'";
echo var_dump($update_qry);
mysqli_query($connection, $update_qry) OR DIE(mysqli_error($connection));
$i++;
}
header('Location: http://www.stockoverflow.com/'); // INSERT approveReject.php ???
}
else {
// Query that retrieves events
$con = "SELECT * FROM evenement WHERE approved = 'no' ORDER BY id";
$result = mysqli_query($connection, $con) or die(mysqli_error($connection));
if ($result) {
$registry = mysqli_num_rows($result) or die (mysqli_error($connection));
if ($registry > 0) {
echo '
<h1 align="center">Events pending approval</h1>
<br><table width="100%" align="center" border="0" border-spacing="2px" cellspacing="1" cellpadding="1">
<form action="" method="POST" >
<tr bgcolor="#3333FF" style="color:white; font-family:Tahoma, Geneva, sans-serif; font-size:15px"">
<th align="center"><strong>Title</strong></th>
<th align="center"><strong>Details</strong></th>
<th align="center"><strong>Category</strong></th>
<th align="center"><strong>Start</strong></th>
<th align="center"><strong>End</strong></th>
<th align="center"><strong>All Day event?</strong></th>
<th align="center"><strong>Approved</strong></th>
</tr>';
$color = "1";
while ($registry = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if ($color == 1) {
echo '<tr bgcolor="#F8F8F8" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color = "2";
} else {
echo '<tr bgcolor="#dcdcdc" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color = "1";
}
echo '
<input type="hidden" value="' . $registry['id'] . '" name="id[]" id="id">
<th div align="center">' . $registry['title'] . '
<td div align="center">' . $registry['details'] . '
<td div align="center">' . $registry['category'] . '
<td div align="center">' . $registry['start'] . '
<td div align="center">' . $registry['end'] . '
<td div align="center"><select name="allDay[]" id="allDay">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<td div align="center"><select name="ap_re[]" id="ap_re">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</tr>';
}
?>
<input type="submit" name="submit" id="button" value="Submit" style="height:1.8em; width:7.3em;" />
<?php
echo '</form>
';
echo '
</form>
</table>';
}
} else {
echo '<h1>There are no new requests to be approved</h1>';
}
}
?>
You should try this, your update syntax might causing you the problem
$udpate_qry = "UPDATE evenement
SET allDay='$allDay', approved='$ap_re'
WHERE id='$id'";
Mr. Radical, you are the man, I was able to update my table with your suggested code, I just needed to add $length = count($allDay) and then change the while loop to while(($id) && ($i < $length)) that made it. Really appreciate your help.

Categories