php while loop not displaying rows after 15 rows - php

Hello I have Two files one file contain the main posts and second file contain load more posts code
Following are the code of main file but in this file i have a problem that i have to loop my all mysql rows in php and i can do it using while loop but when i try this i am getting all rows but after 15 rows it shows only those that are after 15 rows like only 16 row
My main.php code
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$nv_name_split = str_replace('_', ' ', $row['novel_name']);
echo '<br><a href="episode?novel='.$row['novel_name'].'&episode='.$row['episode_num'].'"><figure class="snip1253"><div class="image"><img width="100%"src="images/sneakpeaks/'.$row['sneak_peak'].'" alt="sample59"/></div><figcaption>'.
'<div class="date"><span class="day">'.$row['day'].'</span><span class="month">'.$row['month'].'</span></div><h3>Episode # '.$row['episode_num'].'</h3> <footer>
<div class="writer text-center"><i class="fa fa-book"> '.$nv_name_split.'</i></div>
<div class="love"><i class="fa fa-thumbs-up"></i>'.$row['likes'].'</div>
<div class="comments"><i class="fa fa-comments"></i>'.$row['comments'].'</div></footer></figure></a></figcaption><br><br>';
}
?>
<?php
$stmt_count = $novel_list->runQuery("SELECT episode_num
FROM posts
WHERE just_skpk='0'");
$stmt_count->execute();
while ($rows = $stmt_count->fetch(PDO::FETCH_ASSOC)) {
$count_row = $rows['episode_num'];
}
if ($limit < $count_row) {
echo'<form id="frm_more_post">
<center>
<button type="submit" class="form-control btn btn-load_more" id="frm_btn_more">Load More Posts</button>
</center>
</form>
<br>
<br>';
}else{
echo'<form id="frm_more_post">
<center>
<button type="submit" disabled class="disabled form-control btn btn-load_more" id="frm_btn_more">No More Posts</button>
</center>
</form>
<br>
<br>';
}
and this is my load_more.php code where i post limit of posts using ajax
load_more.php code
$novel_list = new USER();
if (isset($_POST['page'])) {
$limit = $_POST['page']+3;
$stmt = $novel_list->runQuery("SELECT *
FROM posts
WHERE just_skpk='0'
ORDER BY id DESC
LIMIT $limit");
$stmt->execute();
if ($stmt->rowCount(0)) {
echo '<input class="hidden" id="pages" value="'.$limit.'"/>';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$nv_name_split = str_replace('_', ' ', $row['novel_name']);
echo '<br><a data-pages="'.$limit.'" href="episode?novel='.$row['novel_name'].'&episode='.$row['episode_num'].'"><figure class="snip1253"><div class="image"><img width="100%"src="images/sneakpeaks/'.$row['sneak_peak'].'" alt="sample59"/></div><figcaption>'.
'<div class="date"><span class="day">'.$row['day'].'</span><span class="month">'.$row['month'].'</span></div><h3>Episode # '.$row['episode_num'].'</h3> <footer>
<div class="writer text-center"><i class="fa fa-book"> '.$nv_name_split.'</i></div>
<div class="love"><i class="fa fa-thumbs-up"></i>'.$row['likes'].'</div>
<div class="comments"><i class="fa fa-comments"></i>'.$row['comments'].'</div></footer></figure></a></figcaption><br><br>';
}
$stmt_count = $novel_list->runQuery("SELECT episode_num
FROM posts
WHERE just_skpk='0'");
$stmt_count->execute();
while ($rows == $stmt_count->fetch(PDO::FETCH_ASSOC)) {
$count_row = $rows['episode_num'];
}
if ($limit < $count_row) {
echo '<form id="frm_more_post">
<center>
<button type="submit" class="form-control btn btn-load_more" id="frm_btn_more">Load More Posts</button>
</center>
</form>
<br>
<br>';
}else{
echo '<form id="frm_more_post">
<center>
<button type="submit" disabled class="disabled form-control btn btn-load_more" id="frm_btn_more">No More Posts</button>
</center>
</form>
<br>
<br>';
}
}else{
echo 0 ;
}
}
i solve first problem in main.php while loop using double == in while loop but in load_more.php file i cant show more posts button it shows disabled but when i have less than 15 rows in database its working perfectly please help me to show load more posts button in load_more.php.
ThankYou,
Any help will be appreciated.

In your code you are getting episode_num as $count_row, but it should be no of rows. Try below code
$novel_list = new USER();
if (isset($_POST['page'])) {
$limit = $_POST['page']+3;
$stmt = $novel_list->runQuery("SELECT *
FROM posts
WHERE just_skpk='0'
ORDER BY id DESC
LIMIT $limit");
$stmt->execute();
if ($stmt->rowCount(0)) {
echo '<input class="hidden" id="pages" value="'.$limit.'"/>';
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$nv_name_split = str_replace('_', ' ', $row['novel_name']);
echo '<br><a data-pages="'.$limit.'" href="episode?novel='.$row['novel_name'].'&episode='.$row['episode_num'].'"><figure class="snip1253"><div class="image"><img width="100%"src="images/sneakpeaks/'.$row['sneak_peak'].'" alt="sample59"/></div><figcaption>'.
'<div class="date"><span class="day">'.$row['day'].'</span><span class="month">'.$row['month'].'</span></div><h3>Episode # '.$row['episode_num'].'</h3> <footer>
<div class="writer text-center"><i class="fa fa-book"> '.$nv_name_split.'</i></div>
<div class="love"><i class="fa fa-thumbs-up"></i>'.$row['likes'].'</div>
<div class="comments"><i class="fa fa-comments"></i>'.$row['comments'].'</div></footer></figure></a></figcaption><br><br>';
}
$stmt_count = $novel_list->runQuery("SELECT count(*) as cnt
FROM posts
WHERE just_skpk='0'");
$stmt_count->execute();
while ($rows = $stmt_count->fetch(PDO::FETCH_ASSOC)) {
$count_row = $rows['cnt'];
}
if ($limit < $count_row) {
echo '<form id="frm_more_post">
<center>
<button type="submit" class="form-control btn btn-load_more" id="frm_btn_more">Load More Posts</button>
</center>
</form>
<br>
<br>';
}else{
echo '<form id="frm_more_post">
<center>
<button type="submit" disabled class="disabled form-control btn btn-load_more" id="frm_btn_more">No More Posts</button>
</center>
</form>
<br>
<br>';
}
}else{
echo 0 ;
}
}

I think this because of the pagination. In the file load_more.php, you are setting a limit
$limit = $_POST['page']+3;
and in the file, main.php
if ($limit < $count_row)
this is the check.

Related

How to find the difference between event dates in the time line using php?

Actually I am working on the 2002 ( old php project) based on the requirement. So my work is to show the difference between events markers based on the dates by placing some width dynamically in php. So I wrote the code after that it is not even displaying the events markers in the page.
I will provide the code which I wrote for this requirement.
function getEvents($uid)
{
$timeLineQry = mysql_query("select * from timelines where user_id = $uid and status=1 order by id desc");
if(mysql_num_rows($timeLineQry) > 0)
{
$data='';
while($tres = mysql_fetch_assoc($timeLineQry))
{
$data.='<div class="events-chat" id="timeline'.$tres['id'].'">
<button class="btn btn-success" onclick="addEvent('.$tres['id'].')"> Add Event</button>
<div id="" style="float:right;">
<span style="margin-left: 10px;color: #696767;
cursor: pointer;margin-right: 25px;font-size: 16px;font-weight: bolder;font-family: inherit;" onclick="getTimeLine(\''.$tres['title'].'\',\''.$tres['description'].'\',\''.$tres['id'].'\')">'.$tres['title'].'</span>
<i class="glyphicon glyphicon-trash" onclick="deleteTimeline('.$tres['id'].')"></i>
</div>
<div style="clear:both;margin-bottom:35px;"></div>
<div id="eventBlck'.$tres['id'].'">';
//echo "select * from events where user_id = $uid and timeline=".$tres['id'];exit;
$user_exe1 = mysql_query("select * from events where user_id = $uid and timeline=".$tres['id']." order by date asc");
if(mysql_num_rows($user_exe1) > 0) {
$i=1;
$data.='<div class="events-chat-inner">
<div class="all-cht-blk">
<div class="event-border">
<div class="c-dot" style="float:left;position:absolute;left:-4px;top:-4px;"></div>
<div class="c-dot" style="float:right;position:absolute;right:-4px;top:-4px;"></div>
</div>
';
while($res = mysql_fetch_assoc($user_exe1))
{
//Here is the code I just tried to subtract the date of one event from date of another event.
$datecount = count($res['date']);
for($j = 0; $j <= $datecount; $j++){
$previous_event_date = strtotime($res['date'][j]);
$present_event_date = strtotime($res['date'][j+1]);
$distance = $previous_event_date - $present_event_date;
$distance = floor($distance/(60 * 60 * 24));
$case1 = range(0,20);
$case2 = range(1,40);
if(in_array($distance,$case1))
{
$distancestyle = 'width:130px';
}
elseif(in_array($distance,$case2))
{
$distancestyle = 'width:180px';
}
else
{
$distancestyle = 'width:230px';
}
if($i%2==0){$bc='#f45e34';}else if($i%3==0){$bc='#fa454a';}else if($i%4==0){$bc='#3cb54b';}else if($i%5==0){$bc='#03aeac';}else{$bc='#9f388b';}
if($i%2==0)
{
$data.='<div class="event-ct-blk" style='\$distancestyle\'>
<h3 onclick="getData('.$res['id'].',
\''.str_replace("'","\'",$res['name']).'\',
\''.str_replace("'","\'",$res['description']).'\',
\''.date('m-d-Y',strtotime($res['date'])).'\');">'.$res['name'].'</h3>
<p onclick="getData('.$res['id'].',\''.str_replace("'","\'",$res['name']).'\',\''.str_replace("'","\'",$res['description']).'\',\''.date('m-d-Y',strtotime($res['date'])).'\');">'.$res['description'].' </p>
<div class="pointer">
<div class="c-dot"></div>
<div class="p-strip-2" style="height:125px;"></div>
<div class="q-circle-outer" onclick="getData('.$res['id'].',\''.str_replace("'","\'",$res['name']).'\',\''.str_replace("'","\'",$res['description']).'\',\''.date('m-d-Y',strtotime($res['date'])).'\');">
<div class="q-circle" style="background: '.$bc.';"></div>
</div>
<div class="p-strip" style="height:25px;"></div>
<div class="c-dot"></div>
</div>
<p class="q-date" onclick="getData('.$res['id'].',
\''.str_replace("'","\'",$res['name']).'\',
\''.str_replace("'","\'",$res['description']).'\',
\''.date('m-d-Y',strtotime($res['date'])).'\');"> Date: '.date('M-d-Y',strtotime($res['date'])).' </p>
</div>';
}
else
{
$data.='<div class="event-ct-blk" style='\$distancestyle\'>
<h3 onclick="getData('.$res['id'].',\''.str_replace("'","\'",$res['name']).'\',\''.str_replace("'","\'",$res['description']).'\',\''.date('m-d-Y',strtotime($res['date'])).'\');">'.$res['name'].'</h3>
<p onclick="getData('.$res['id'].',\''.str_replace("'","\'",$res['name']).'\',\''.str_replace("'","\'",$res['description']).'\',\''.date('m-d-Y',strtotime($res['date'])).'\');">'.$res['description'].' </p>
<div class="pointer">
<div class="c-dot"></div>
<div class="p-strip" style="height:25px;"></div>
<div class="q-circle-outer" onclick="getData('.$res['id'].',\''.str_replace("'","\'",$res['name']).'\',\''.str_replace("'","\'",$res['description']).'\',\''.date('m-d-Y',strtotime($res['date'])).'\');">
<div class="q-circle" style="background: '.$bc.';"></div>
</div>
<div class="p-strip-2" style="height:125px;"></div>
<div class="c-dot"></div>
</div>
<p class="q-date" onclick="getData('.$res['id'].',\''.str_replace("'","\'",$res['name']).'\',\''.str_replace("'","\'",$res['description']).'\',\''.date('m-d-Y',strtotime($res['date'])).'\');"> Date: '.date('M-d-Y',strtotime($res['date'])).' </p>
</div>';
}
$i++;
}
$data.='</div></div>';
}
else
{
$data.='<div class="event-border" style="width:96%;">
<div class="c-dot" style="float:left;position:absolute;left:-4px;top:-4px;"></div>
<div class="c-dot" style="float:right;position:absolute;right:-4px;top:-4px;"></div>
</div><div style="text-align:center;margin-top:78px;font-size: 18px;"><p>No events added.</p></div>';
}
$data.='</div></div>';
}
}
else
{
$data='<div class="events-chat" id="timeline1"> <button class="btn btn-success" onclick="addEvent(1)"> Add Event</button><div style="clear:both;margin-bottom:35px;"></div><div id="eventBlck1"> <div class="event-border" style="width:96%;">
<div class="c-dot" style="float:left;position:absolute;left:-4px;top:-4px;"></div>
<div class="c-dot" style="float:right;position:absolute;right:-4px;top:-4px;"></div>
</div><div style="text-align:center;margin-top:78px;font-size: 18px;"><p>No events added.</p></div></div></div>';
}
}//end for loop
return $data;
}
In this code I place the code which I marked as the comments and actually this is the code I have present but after working on this file events are not showing canyou please see the issue in this code or any errors.

Answering To Telegram Users Separately

I'm working on a Content Management System using PHP for controlling over my Telegram Bot. Basically what I have done till now is that I can read the messages that people has sent to my Telegram Bot and answer to them.
In order to do that, I coded this:
<?php
$botToken = '423495534:asdsadsadasdsadsa';
$website = 'https://api.telegram.org/bot'.$botToken;
$update = file_get_contents($website."/getUpdates");
$updateArray = json_decode($update, TRUE);
$info = file_get_contents($website."/getme");
$infoArray = json_decode($info, TRUE);
$num = count($updateArray["result"]);
$sender_ids = array();
$sender_infos = array();
for($i=0;$i<$num;$i++){
$sender_id = $updateArray["result"][$i]["message"]["from"]["id"];
$sender_isbot = $updateArray["result"][$i]["message"]["from"]["is_bot"];
$sender_fname = $updateArray["result"][$i]["message"]["from"]["first_name"];
$sender_lname = $updateArray["result"][$i]["message"]["from"]["last_name"];
$sender_uname = $updateArray["result"][$i]["message"]["from"]["username"];
$sender_type = $updateArray["result"][$i]["message"]["chat"]["type"];
$sender_msg = $updateArray["result"][$i]["message"]["text"];
$sender_date = $updateArray["result"][$i]["message"]["date"];
if(false === $key = array_search($sender_id, $sender_ids)){
$sender_ids[] = $sender_id;
$sender_infos[] = [
'sender_id' => $sender_id,
'sender_isbot' => $sender_isbot,
'sender_fname' => $sender_fname,
'sender_lname' => $sender_lname,
'sender_uname' => $sender_uname,
'sender_type' => $sender_type,
'sender_msg' => [$sender_msg],
'sender_date' => [$sender_date]
];
}else{
$sender_infos[$key]['sender_msg'][] = $sender_msg;
$sender_infos[$key]['sender_date'][] = $sender_date;
}
}
$num2 = count($sender_ids);
for($j=0;$j<$num2;$j++){
$id = $sender_infos[$j]["sender_id"];
$first_name = $sender_infos[$j]["sender_fname"];
$last_name = $sender_infos[$j]["sender_lname"];
$username = $sender_infos[$j]["sender_uname"];
$messages = $sender_infos[$j]["sender_msg"];
$acc_type = $sender_infos[$j]["sender_type"];
$isbot = $sender_infos[$j]["sender_isbot"];
$num1 = count($messages);
echo '
<div class="col-md-3">
<div class="box box-danger direct-chat direct-chat-danger">
<div class="box-header with-border">
<h3 class="box-title">'.$first_name.'</h3>
<sup>'.$acc_type.' - '.$isbot.'</sup>
<div class="box-tools pull-right">
<span data-toggle="tooltip" title="'.$num1.' New Messages" class="badge bg-red">'.$num1.'</span>
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-toggle="tooltip" title="Contacts" data-widget="chat-pane-toggle">
<i class="fa fa-comments"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
<div class="direct-chat-messages">
<div class="direct-chat-msg">
<div class="direct-chat-info clearfix">
<span class="direct-chat-name pull-left">';
echo '<strong>'.$first_name.'</strong>';
echo '
</span>
</div>';
for($i=0;$i<$num1;$i++){
echo '<div class="direct-chat-text">';
$text = $sender_infos[$j]["sender_msg"][$i];
if($text[0] === '/') {
echo ''.$text.'';
}else{
echo $text;
}
echo '<span class="direct-chat-timestamp pull-right">';
// echo date('l', $sender_infos[$j]["sender_date"][$i]);
echo '</span>';
echo '</div>';
}
echo '
</div>
<div class="direct-chat-msg right">
<div class="direct-chat-info clearfix">
<span class="direct-chat-name pull-right">'.$bot_fname.'</span>
</div>
<img class="direct-chat-img" src="
';
if ($dataSet->GetAvatar() != NULL){
echo $dataSet->GetAvatar();
}else{
echo "img/noprofilepic.jpg";
}
echo '
" alt="Message User Image">
';
$num3 = count($request_params["text"]);
foreach($_SESSION['messages'] as $reply){
echo '<div class="direct-chat-text">';
echo $reply;
echo '</div>';
}
echo '
</div>
</div>
<div class="direct-chat-contacts">
<ul class="contacts-list">
<li>
<a href="#">
<div class="contacts-list-info">
<span class="contacts-list-name">
';
echo $first_name = $sender_infos[$j]["sender_fname"];
echo ' </br> ';
echo $last_name = $sender_infos[$j]["sender_lname"];
echo ' </br> ';
echo ' <a target="_blank" href="http://www.t.me/'.$username.'">'.$username.'</a>';
echo ' </br> ';
echo ' <small class="contacts-list-date pull-right">'.$id.'</small>
</span>
<span class="contacts-list-msg">QUOTE</span>
</div>
</a>
</li>
</ul>
</div>
</div>';
if (isset($_POST['send'])){
$pm = $_POST['message'];
array_push($_SESSION['messages'], $pm);
$request_params = [
'chat_id' => $id,
'text' => $pm
];
echo "<META HTTP-EQUIV='Refresh' Content='0; URL=telegrambots.php?user_name=".$user_name."'>";
$request_url = 'https://api.telegram.org/bot' . $botToken . '/sendMessage?' . http_build_query($request_params);
$response = file_get_contents($request_url);
}
echo '
<div class="box-footer">
<form action="" method="post">
<div class="input-group">
<input type="text" name="message" placeholder="Write your direct message" class="form-control">
<span class="input-group-btn">
<input name="send" type="submit" class="btn btn-danger btn-flat"/>
</span>
</div>
</form>
</div>
</div>
</div>
';
}
?>
Basically what it does is that it grabs the latest updates of my Bot and count the number of results and save it in $num. Then with a for loop I tried to divide every information about the user who has sent message. After that I store all the required information of sender in a separated array called $sender_infos. And the next for loop shows a basic Chat Box depending on the number of users.
This whole thing makes this:
So it works fine and perfect but the problem is that, whenever I try answering to one conversation, it sends the message to all the available users. This issue comes from the for loop which divides every users by the Chat Box.
However what I want to do is to send a SINGLE DIRECT MESSAGE to a CUSTOM user and NOT ALL OF THEM.
I hope I have explained my problem well, so you could understand. If not, please comment me for more information and additional updates.
NOTE: I don't want a quick answer to this question. Because I'm facing this for several days and I don't know how to solve it. Please make sure you understand what I'm asking for and then add your suggest.
Thanks in advance...

php - Data does not insert into table using checkbox

the checkbox is not working.. I could not insert new data into the table
there is nothing happen when I click the "add to cart" button.
<div class="carousel-inner">
<div class="item">
<ul class="thumbnails">
<?php
$query = mysql_query("SELECT * FROM product where pro_category='3' LIMIT 0,4 ");
while ($data = mysql_fetch_assoc($query)):
?>
<li class="span3">
<div class="product-box">
<span class="sale_tag"></span>
<?php echo '<p><img src="admin/pro_image/'.$data['image'].'" /></p>'; ?>
<?php echo ''.$data['name_nl'].''; ?>
<br/>
<td><input class='minuman' type='checkbox' name='add[]' value='<?php echo '<a class="btn btn-success" href="cart.php?add='.$data['code'].'" class="category">
</a>'; ?>'></td>
<p class="price"><?php echo 'RM '.$data['price']; ?></p>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
</div>
<ul class="thumbnails" align="center">
<div id="single_product" align="center">
<?php echo '<a class="btn btn-success" href="cart.php?add='.$data['code'].'" class="category">Add to Cart</a>'; ?>
</div>
</ul>
this is my code for insert cart:
if(isset($_GET['add'])){
$id = $_GET['add'];
$qt = mysql_query("SELECT code, quantity FROM product WHERE code='$id'");
while($qt_row = mysql_fetch_assoc($qt)){
if($qt_row['quantity'] != $_SESSION['cart_'.$_GET['add']] && $qt_row['quantity'] > 0){
$_SESSION['cart_'.$_GET['add']]+='1';
header("Location: keranjang.php");
} else {
echo '<script language="javascript">alert("Stok produk tidak mencukupi!"); document.location="index.php";</script>';
}
}
}
According to PHP Documentation, mysql_query is deprecated since PHP 5.5.0, and removed in PHP 7.0.0. You may use mysqli or PDO instead.
This row might cause problems, because it's not escaped:
<td><input class='minuman' type='checkbox' name='add[]' value='<?php echo '<a class="btn btn-success" href="cart.php?add='.$data['code'].'" class="category">
</a>'; ?>'></td>
and this is one of the ways to escape:
<td>
<input class="minuman" type="checkbox" name="add[]" value="<?php echo '<a class="btn btn-success" href="cart.php?add=' . $data['code'] . '" class="category"></a>'; ?>" />
</td>
About "add to cart" - You can store the products in the session or in a cookie (I think cookies can be a better option), and then show them in the cart.

delete image ins database mysql from dashboard php

hi i need help i have problem in my code and i can't figure the solutions please help me .
this is the dashboard:
image dashboard
and this is problem after click on delete:
delete problem
and this is my code php of posts file:
<?php
/*
===========================================================
=== Manage Members Page ===
=== You can add | edit | delete Members from here ===
===========================================================
*/
session_start();
if (isset($_SESSION['Username'])) {
include 'init.php';
$pageTitle = 'Posts';
$do = isset($_GET['do']) ? $_GET['do'] : 'Manage' ;
//Start Manage Page
if ($do == 'Manage'){ // Manage Members Page
$sort = 'ASC';
$sort_arry = array('ASC', 'DESC');
if(isset($_GET['sort']) && in_array($_GET['sort'], $sort_arry)) {
$sort = $_GET['sort'];
}
$stmt2 = $con->prepare("SELECT * FROM posts ORDER BY Ordering $sort");
$stmt2->execute();
$rows = $stmt2->fetchAll();
?>
<h1 class="text-center"> Manage Posts </h1>
<div class="container categories">
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-edit"></i> Manage Posts
<div class="ordering pull-right">
<i class="fa fa-sort"> </i>Ordering: [
<a class="<?php if ($sort == 'ASC') { echo 'active'; } ?>" href="?sort=ASC">Asc </a> |
<a class="<?php if ($sort == 'DESC') { echo 'active'; } ?>" href="?sort=DESC">Desc </a>
]
</div>
</div>
<div class="row">
<?php
foreach ($rows as $image) {
echo '<div class="col-md-3 col-sm-4 "><div class="thumbnail">';
echo '<h2 class="h4">'.$image['Name']. '</h2><div class="main">';
echo '<img src="data:image;base64,'.$image['Image'].' " alt="image name" title="image title" width="255" heigth="255">';
echo '</div>';
echo '<table class="table table-bordered">';
echo '<tr>';
echo '<td>' . "<a href='posts.php?do=Edit&id=". $image['ID'] ."' class='btn btn-xs btn-primary'><i class='fa fa-edit'></i> edit</a>" . '</td>';
echo '<td>' . "<a href='posts.php?do=Delete&id=". $image['ID'] ."' class='btn btn-xs btn-danger'><i class='fa fa-close'></i> Delete</a>" . '</td>';
echo '</tr>';
echo '</table>';
echo '</div>';
echo '</div>';
}
?>
</div>
<?php } elseif ($do == 'Add') { //add Member page ?>
<h1 class="text-center"> ajouter un nouveau post </h1>
<div class="container">
<form class="form-horizontal" enctype="multipart/form-data" action="?do=Insert" method="POST">
<!-- start Username fieled -->
<div class="form-group">
<label class="col-sm-2 control-label">Titre</label>
<div class="col-sm-10 col-md-8">
<input type="text" name="image-name" class="form-control" autocomplete="off" placeholder="username pour se connecter dans le site Web" required />
</div>
</div>
<!-- end Username fieled -->
<!-- start Password fieled -->
<div class="form-group">
<label class="col-sm-2 control-label">Image</label>
<div class="col-sm-10 col-md-8">
<input type="file" name="image" class="form-control" placeholder="mot de passe doit ĂȘtre difficile et complexe" required/>
</div>
</div>
<!-- end Password fieled -->
<!-- start Full name fieled -->
<div class="form-group">
<label class="col-sm-2" for="categorie">Categories:</label>
<div class="col-sm-10 col-md-8">
<select class="form-control" name="categorie">
<?php
$stmt = $con->prepare("SELECT * FROM `categories`");
// Execute the Statments
$stmt->execute();
// Assign to variable
$rows = $stmt->fetchAll();
?>
<?php
foreach ($rows as $cat) {
echo "<option value='" . $cat['ID'] . "'>". $cat['Name'] . "</option>";
}
?>
</select>
</div>
</div>
<!-- end Full name fieled -->
<!-- start submit fieled -->
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="Ajouter" class="btn btn-primary" />
</div>
</div>
<!-- end submit fieled -->
</form>
</div>
<?php
} elseif ($do == 'Insert') {
//insert Members Page
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
echo "<h1 class='text-center'> insert an post </h1>";
echo "<div class='container'>";
// Get variable from the form
$name = $_POST['image-name'];
$image= addslashes($_FILES['image']['tmp_name']);
$image= file_get_contents($image);
$image= base64_encode($image);
$cat = $_POST['categorie'];
//validate the form
$formErrors = array();
if (strlen($name) < 4) {
$formErrors[] = "title name cant be less then <strong> 4 caracter</strong>";
}
if (strlen($name) > 20) {
$formErrors[] = "title name cant be More then <strong> 20 caracter</strong>";
}
if (empty($name)) {
$formErrors[] = "Username Cant Be <strong>Empty</strong>";
}
// loop into eroos array and echo it
foreach ($formErrors as $Error) {
echo "<div class='alert alert-danger'>" . $Error . "</div>";
}
// check if There is no error procced the operations
if (empty($formErrors)) {
// check if user exist in database
$check = checkItem("Username", "users", $user);
if ($check == 1) {
$theMsg = "<div class='alert alert-danger'> Sorry this user is exist </div>";
redirectHome($theMsg, 'back');
} else {
// Insert User info into database
$stmt = $con->prepare("INSERT INTO posts(Name, Image, Cat_id)
VALUES (:name, :image, :cat)");
$stmt->execute(array(
'name' => $name,
'image' => $image,
'cat' => $cat,
));
// echo success message
$theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' Record Inserted </div> ';
redirectHome($theMsg, 'back', 5);
}
}
} else {
echo "<div class='container'>";
$theMsg = '<div class="alert alert-danger"> Sorry you cant browse this page directely </div>';
redirectHome($theMsg, 'back', 5); // 6 is secend of redirect to page in function
echo "</div>";
}
echo "</div>";
} elseif ($do == 'Edit') { // Edit Page
//check if GET request userid Is numeric & Get The integer value of it
$post = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;
//sellect All Data Depend On This ID
$stmt = $con->prepare("SELECT * FROM posts WHERE ID = ? LIMIT 1");
// execute Query
$stmt->execute(array($post));
//fetch the Data
$row = $stmt->fetch();
// The row count
$count = $stmt->rowCount();
// If Ther's Such Id show The Form
if ($count > 0) { ?>
<h1 class="text-center"> Modifier Post </h1>
<div class="container">
<form class="form-horizontal" enctype="multipart/form-data" action="?do=Update" method="POST">
<div class="col-md-6 col-md-offset-3 panel">
<input type="hidden" name="id" value="<?php echo $_GET['id']; ?>
<!-- start title fieled -->
<div class="form-group">
<label class="col-sm-2 control-label">Titre</label>
<div class="col-sm-10 col-md-8">
<input type="text" name="name" class="form-control" autocomplete="off" required value="<?php echo $row['Name']; ?>" >
</div>
</div>
<!-- end title field -->
<!-- start image filed -->
<div class="form-group">
<label class="col-sm-2 control-label">image</label>
<div class="col-sm-10 col-md-8">
<input type="file" name="image" class="form-control" />
</div>
</div>
<!-- end image filed -->
<!-- start Categories filed -->
<div class="form-group">
<label class="col-sm-2" for="categorie">Categories:</label>
<div class="col-sm-10 col-md-8">
<select class="form-control" name="categorie">
<?php
$stmt = $con->prepare("SELECT * FROM `categories`");
// Execute the Statments
$stmt->execute();
// Assign to variable
$rows = $stmt->fetchAll();
?>
<?php
foreach ($rows as $cat) {
echo "<option value='" . $cat['ID'] . "'>". $cat['Name'] . "</option>";
}
?>
</select>
</div>
</div>
<!-- Categories end-->
<!-- start submit fieled -->
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="sauvegarder" class="btn btn-primary" />
</div>
</div>
<!-- end submit fieled -->
</div>
</form>
</div>
<?php
// if there's No Such id Show Error Message
} else {
echo "<div class='container'>";
$theMsg = "<div class='alert alert-danger'>Theres is no such Id</div>";
redirectHome($theMsg);
echo "</div>";
}
} elseif ($do == 'Update') {
echo "<h1 class='text-center'> mis a jour Membre </h1>";
echo "<div class='container'>";
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// Get variable from the form
$id = $_POST['id'];
$name = $_POST['name'];
$image = addslashes($_FILES['image']['tmp_name']);
$image = file_get_contents($image);
$image = base64_encode($image);
$cat = $_POST['categorie'];
//validate the form
$formErrors = array();
if (empty($name)) {
$formErrors[] = "<div class='alert alert-danger'>Username Cant Be <strong>Empty</strong> </div>";
}
if (empty($image)) {
$formErrors[] = "<div class='alert alert-danger'>FullName Cant Be <strong>Empty</strong></div>";
}
if (empty($cat)) {
$formErrors[] = "<div class='alert alert-danger'>Email Cant Be <strong>Empty</strong></div>";
}
// loop into eroos array and echo it
foreach ($formErrors as $Error) {
echo $Error;
}
// check if There is no error procced the operations
if (empty($formErrors)) {
// Update The Database With This Info
$stmt = $con->prepare("UPDATE posts SET Name = ? , Image = ? , Cat_id = ? WHERE ID = ?");
$stmt->execute(array($name, $image, $cat, $id));
// echo success message
$theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' Record Updated </div> ';
redirectHome($theMsg, 'back');
}
} else {
$theMsg = '<div class="alert alert-danger">Sorry you cant browse this page directely </div>';
redirectHome($theMsg);
}
echo "</div>";
}
elseif ($do == 'Delete') { // Delete Member Page
echo "<h1 class='text-center'> Delete Membre </h1>";
echo "<div class='container'>";
//check if GET request userid Is numeric & Get The integer value of it
$id = isset($_GET['id']) && is_numeric($_GET['id']) ? intval($_GET['id']) : 0;
//sellect All Data Depend On This ID
$check = checkItem('id', 'posts', $id);
// If Ther's Such Id show The Form
if ($check > 0) {
$stmt = $con->prepare("DELETE FROM users WHERE ID = :id");
$stmt->bindParam(":id", $id);
$stmt->execute();
$theMsg = "<div class='alert alert-success'>" . $stmt->rowCount() . ' Record Deleted </div> ';
redirectHome($theMsg);
} else {
$theMsg = "<div class='alert alert-danger'>This id not exist</div>";
redirectHome($theMsg);
}
echo "</div>";
}
include $tpl . 'footer.php';
} else {
header('Location: index.php') ;
exit();
}
from the error, id is the problem.
isset($_GET['id']) && is_numeric($_GET['id'])
i think what u want is
(isset($_GET['id']) && is_numeric($_GET['id']) )//close parantheses in wrong position

Checkbox button not updating a selected row?

My selector when selected all is updating only 1 row? however if i select a row, it cannot be updated? please see my code if something's wrong, needed help.
This is the form:
<form action="read_message.php" method="post">
<div class="pull-right">
<button class="btn btn-info" name="read"><i class="icon-check"></i> Read</button>
Check All <input type="checkbox" name="selectAll" id="checkAll" />
<script>
$("#checkAll").click(function () {
$('input:checkbox').not(this).prop('checked', this.checked);
});
</script>
</div>
<ul class="nav nav-pills">
<li class="active"><i class="icon-envelope-alt"></i>inbox</li>
<li class=""><i class="icon-envelope-alt"></i>Send messages</li>
</ul>
<?php
$query_announcement = mysql_query("select * from message_received
LEFT JOIN user ON user.user_id = message_received.user_id
where message_received.receiver_id = '$session_id' order by date_sent DESC
")or die(mysql_error());
$count_my_message = mysql_num_rows($query_announcement);
if ($count_my_message != '0'){
while($row = mysql_fetch_array($query_announcement)){
$id = $row['message_id'];
$id_2 = $row['message_id'];
$fn = $row['firstname'];
$ln = $row['lastname'];
$status = $row['message_status'];
$sender = $row['user_id'];
$sender_name = $fn.' '.$ln;
$receiver = $row['receiver_id'];
?><div class="alert alert-info">
<div class="post" id="del<?php echo $id; ?>">
<div class="message_content">
<?php echo $row['content']; ?>
</div>
<div class="pull-right">
<?php if ($status == 'read'){
}else{ ?>
<input id="" class="" name="selector[]" type="checkbox" value="<?php echo $id; ?>">
<?php } ?>
</div>
<hr>
Send by: <strong><?php echo $fn.' '.$ln; ?></strong>
<i class="icon-calendar"></i> <?php echo $row['date_sent']; ?>
<div class="pull-right">
<a class="btn btn-link" href="#reply<?php echo $id; ?>" data-toggle="modal" ><i class="icon-reply"></i> Reply </a>
</div>
<div class="pull-right">
<a class="btn btn-link" href="#<?php echo $id; ?>" data-toggle="modal" ><i class="icon-remove"></i> Remove </a>
<?php include("remove_inbox_message_modal.php"); ?>
<?php include("reply_inbox_message_modal_user.php"); ?>
</div>
</div>
</div>
<?php }}else{ ?>
<div class="alert alert-info"><i class="icon-info-sign"></i> No Message Inbox</div>
<?php } ?>
</form>
However, nevermind the isset for reply, the problem is the first function for isset read, what's wrong with the query? this is the function for the selector, read_message.php,
<?php include('connect.php'); ?>
<?php
include('session.php');
if (isset($_POST['read'])){
$id=$_POST['selector'];
$N = count($id);
for($i=0; $i < $N; $i++)
{
$result = mysql_query("update message_received set message_status = 'read' where message_id='$id[$i]'");
}
echo "<script> alert('Successfully Updated') </script>";
echo " <script>location.replace('messages.php')</script>";
}
?>
<?php
if (isset($_POST['reply'])){
$sender = $_POST['sender'];
$receiver = $_POST['receiver'];
$id2=$_POST['id2'];
$qwe = $_POST['qwe'];
mysql_query("update message_received set message_status = 'read' where message_id='$id2'");
mysql_query("insert into message_received (user_id,receiver_id,content,date_sent) values('$receiver','$sender','$qwe',NOW())")or die(mysql_error());
mysql_query("insert into message_sent (receiver_id,user_id,content,date_sent) values('$sender','$receiver','$qwe',NOW())")or die(mysql_error());
echo "<script> alert('Your message has been sent') </script>";
echo " <script>location.replace('messages.php')</script>";
?>
<script>
alert('Message Sent');
window.location ="messages.php";
</script>
<?php
}
?>

Categories