I have got a page in which I'm getting a users all friends posts now I want to get only 15 post first and then get I wanna add a load more button and then load 15 more posts how should I do that.
This is my code
$query = "SELECT ph.likes, ph.image_url,ph.email,ph.username,ph.uid ,ph.id
FROM photos as ph
inner join followers as fol
on fol.user_id = ph.uid
where fol.uid = '$id'
ORDER BY ph.image_url DESC ";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
while ($users = mysqli_fetch_assoc($fire)) {
$likes = $users['likes'];
$username = $users['username'];
$uid = $users['uid'];
$pixid = $users['id'];
}
}
?>
<?php
$query = "SELECT ph.ext, ph.likes,ph.desccription, ph.image_url,ph.email,ph.username,ph.uid ,ph.id,ph.avatar_path
FROM photos as ph
inner join followers as fol
on fol.user_id = ph.uid
where fol.uid = '$id'
ORDER BY ph.image_url DESC ";
$fire = mysqli_query($con,$query) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire)>0) {
while ($users = mysqli_fetch_assoc($fire)) {
$likes = $users['likes'];
$description = $users['desccription'];
$username = $users['username'];
$uid = $users['uid'];
$pixid = $users['id'];
$avatar_path5 = $users['avatar_path'];
?>
<div class="all" >
<div class="card" >
<div class="float" >
<div class="avatar" >
<img src="<?php echo $avatar_path5; ?>" width="100%" class="avatar">
</div>
<div class="username" style="font-weight: 600; size: 14px; text-decoration: none; color: black !important; ">
<p style="color: black !important;"><?php echo "<div><a href='users?id=".$users['uid']."' style='color: black !important; '>
<h3>".$users['username']."</h3>
</div></a>"; ?></p>
</div>
</div>
<?php
if ($users['ext']=='mp4') {
?>
<video class="videos" controlsList="nofullscreen nodownload" style="width: 100%; height:100%;" controls autoplay="true" muted>
<source src="<?php echo $users['image_url']?>" type="video/mp4">
</video>
<?php
}else{
?>
<img src="<?php echo $users['image_url']?>" alt="Avatar" style="width:100%;">
<?php }
?>
<div class="container">
<h4><b><?php echo "<div><a href='users?id=".$users['uid']."'>
</div></a>";?></b></h4>
</div>
<div style="padding: 2px; margin-top: 5px;">
<?php
if (isset($_POST['liked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)
or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "INSERT INTO likes (user_id,username, post_id,avatar_path) VALUES ($id, '$fullname', $postid,'$activeavatar')")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n+1 WHERE id=$postid")or die(mysqli_error($con));
echo $n+1;
exit();
}
if (isset($_POST['unliked'])) {
$postid = $_POST['postid'];
$result = mysqli_query($con, "SELECT * FROM photos WHERE id=$postid")or die(mysqli_error($con));
$row = mysqli_fetch_array($result)or die(mysqli_error($con));
$n = $row['likes'];
mysqli_query($con, "DELETE FROM likes WHERE post_id=$postid AND user_id=$id")or die(mysqli_error($con));
mysqli_query($con, "UPDATE photos SET likes=$n-1 WHERE id=$postid")or die(mysqli_error($con));
echo $n-1;
exit();
}
?>
</div>
<script type="text/javascript">
$('video').each(function(){
if ($(this).is(":in-viewport")) {
$(this)[0].play();
} else {
$(this)[0].pause();
}
})
</script>
<div>
<?php
// determine if user has already liked this post
$results = mysqli_query($con, "SELECT * FROM likes WHERE user_id=$id AND post_id=".$users['id']."")or die(mysqli_error($con));
if (mysqli_num_rows($results) == 1 ): ?>
<!-- user already likes post -->
<span class="unlike fas fa-heart " data-id="<?php echo $users['id']; ?>"></span>
<span class="like hide far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<?php else: ?>
<!-- user has not yet liked post -->
<span class="like far fa-heart" onclick="PlaySound()" data-id="<?php echo $users['id']; ?>"></span>
<span class="unlike hide fas fa-heart " data-id="<?php echo $users['id']; ?>"></span>
<?php endif ?>
<a class="com" style="color: #929292 !important; " href="show?post_id=<?php echo $users['id'];?>" style=""> <span class="glyphicon glyphicon-comment trigger" ></span>comments</a>
<div class="wink" style=" color: black;
width: 30px;
height: 30px;
margin-top: -20px;
margin-left: 209px;
" > <?php
?></div>
<div class="float2">
<div class="react2" id="react2" data-rowid="<?php echo $pixid?>"><?php
$query2 = "SELECT * FROM gormint where post_id = $pixid and user_id= $id3";
$fire2 = mysqli_query($con,$query2) or die("can not fetch data from database ".mysqli_error($con));
$query3 = "SELECT * FROM bhai where post_id = $pixid and user_id= $id3";
$fire3 = mysqli_query($con,$query3) or die("can not fetch data from database ".mysqli_error($con));
$query4 = "SELECT * FROM famer where post_id = $pixid and user_id= $id3";
$fire4 = mysqli_query($con,$query4) or die("can not fetch data from database ".mysqli_error($con));
$query5 = "SELECT * FROM muskan where post_id = $pixid and user_id= $id3";
$fire5 = mysqli_query($con,$query5) or die("can not fetch data from database ".mysqli_error($con));
if (mysqli_num_rows($fire2)>0) {
echo "<img src='gormint.jpg' class='gormint2' style='width:30px; height:30px;' >";
}elseif (mysqli_num_rows($fire3)>0) {
echo "<img src='bhai.jpg' class='bhai2' style='width:30px; height:30px;'>";
}elseif (mysqli_num_rows($fire4)>0) {
echo "<img src='famer.jpg' class='bhai2' style='width:30px; height:30px;'>";
}elseif (mysqli_num_rows($fire5)>0) {
echo "<img src='bancho.jpg' class='bhai2' style='width:30px; height:30px;'>";
} else{
echo "<img src='wink.png' class='wink2' style='width:30px; height:30px;'>";
}?>
</div>
<div class="flipClass" style="display: flex;" data-rowid="<?php echo $pixid?>" id="flip">react</div>
<div class="panelClass" style="" id="panel" data-rowid="<?php echo $pixid?>">
<input type="image" onclick="PlaySound2()" id="display" data-value="<?php echo $users['id'];?>" src="gormint2.jpg" class="close2 display gormint animated bounceIn " >
<input type="image" onclick="PlaySound3()" data-value="<?php echo $users['id'];?>" data-rowid="<?php echo $pixid?>" id="display2" src="bhai.jpg" class="close2 display2 bhai animated bounceIn">
<input type="image" data-rowid="<?php echo $pixid?>" onclick="PlaySound4()" data-value="<?php echo $users['id'];?>" id="display3" src="famer.jpg" class="close2 display3 bhai animated bounceIn">
<input type="image" onclick="PlaySound5()" data-value="<?php echo $users['id'];?>" id="display4" src="bancho.jpg" class="close2 display4 bhai animated bounceIn">
</div>
</div>
<div class="count2 ">
<span id="count" style="margin-top: -5%;" class="likes_count"><?php echo $users['likes']; ?> likes</span><?php
if ($description=='') {
echo "";
}else{ echo '<p style="margin-bottom: 5%" class="written"> <b>'.$users["username"].'</b> '.$description;'</p>';
}
?>
</div>
<form method="POST" action="" >
<div class="commentdiv">
<input type="hidden" name="id" id="id" class="id" value="<?php echo $pixid;?>">
<input type="hidden" name="username" id="username" value="<?php echo $activeusername;?>">
<input type="hidden" name="uid" id="uid" value="<?php echo $id3;?>">
<textarea style="" name="comment" id="comment" class="comment" placeholder=" comment here"></textarea>
<button type="button" style="background-color: Transparent;
background-repeat:no-repeat;
border: none;
cursor:pointer;
overflow: hidden;
color: #3897f0; font-weight:600;" class="submit" id="button_id">comment</button>
</div>
</form>
</div>
<div id="comments">
<?php
$sql = "SELECT * FROM comment where post_id='$pixid' limit 3";
$results = mysqli_query($con,$sql);
if (mysqli_num_rows($results)>0) {
while ($row = mysqli_fetch_assoc($results)) {
$commentid = $row['id'];
$comment = $row['comment'];
$string = covertHashtags($comment);
echo "<p class='written'>";
echo "<a href='users2?id=".$row['user_id']."' style='color:black !important;'><b>".$row['username']."</b></a>";
echo " ".$string;
$sql3 = "SELECT * FROM comment where id ='$commentid' and user_id='$id' order by comment desc limit 5 ";
$results3 = mysqli_query($con,$sql3);
if (mysqli_num_rows($results3)>0) {
echo "<div class='dropdown'>
<img src='ellipsis.png' class='dots'>
<div class='dropdown-content'>
<br><p class='delete' data-delete=".$commentid.">delete</p>
</div>
</div>";
}
else{
echo "";
}
echo "</p>";
}
}else{
echo "";
}
?>
</div>
</div>
<br><br>
<?php } ?>
now what should I do to add a load more button after first 15 posts and when user clicks on that button I want to load more 15 posts and there is also one more problem I have a set a refresh functionality through j query whenever a user clicks on like button or comments the page refreshes so would this cause any problem?
Related
In our page, there is 1 input that has to be manually enter by user. Upon what he has entered, some records has been shown and there is Disable or Enable beside each results(Example, I input 12345 and results are A,B,C,D, 4 results and beside of those results, there will be disable button side.). Currently, what is happening is when I click Disable or Enable, the page reload automatically and I can't find a way to fix it. Is there anyway to stop reloading? I use window.history.go(-1). it update the database but the page keeps reload every time I disable or enable.
Below is HTML codes.
<body onload="OnLoad()">
<form class="form-horizontal" name="translot" method="post">
<form id="form" method="post">
<div class="col" style="width: 580px; font-weight: bold; border: 1px solid SteelBlue; color: #6699CC; margin-left: 170px;">
<p style="background-color: #6699CC; font-size: 16px; width: 580px; padding: 5px; color: #FFF; font-weight: bold; margin-left: -1px;margin-top: -1px;"></p>
<div class="form-group">
<br>
<label class="control-label col-sm-4" for="productlotid">Lot ID:</label>
<div class="col-sm-4">
<p class="form-control-static" style="margin-top: -6px;">
<input type="text" class="form-control" id="productlotid" name="productlotid" onkeydown="return KeyDown()" onkeyup="this.value=this.value.toUpperCase()" onkeypress="return searchKeyPress(event)" min="1" placeholder="Enter Lot ID" value="<?php echo $productlotid; ?>">
</p>
<?php
include("correlationwafer_result.php");
?>
</div>
<div id="result" style="display: inline-table; margin-left: 150px; margin-top: 22px;"></div>
<!--div id="result" ></div-->
<div class="col-sm-10">
</div>
<br>
</div>
</div>
<br>
<div style="margin-right: 65px;">
<p align="center"><button type="button" class="btn btn-info " name="cmdSubmit" id="cmdSubmit" onclick="Submit()"><b>Go</b></button> <button type="button" class="btn btn-warning" name="cmdReset" onclick="Reset()"><b>Reset</b></button></p>
</div>
</form>
</form>
Below is php code from correlationwafer_result.php.
<?php
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();
$productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:'';
//$sql1 = "Update * FROM productdb.tbl_correlationwafer WHERE lotid = '$productlotid' ORDER BY lotid and zone_enable='0'";
$sql = "SELECT * FROM productdb.tbl_correlationwafer WHERE lotid = '$productlotid' ORDER BY product asc, zone asc";
$result1 = mysqli_query($conn,$sql);
echo "<table id='corwafer'>";
$arr = array();
while ($row = mysqli_fetch_assoc($result1)) {
$field1name = $row["lotid"];
$field2name = $row["product"];
$field3name = $row["ewsflow"];
$field4name = $row["zone"];
$field5name = $row["zone_enable"];
$key = $field1name + ":" + $field2name + ":" + $field3name;
if (!in_array($key, $arr)){
array_push($arr, $key);
}
?>
<form action='test.php' method='post' enctype='multipart/form-data'>
<?php
echo "<tr>";
echo "<td>";
if($field5name == 1){
echo "<input type='hidden' id='chkzone' name='chkzone' value='$field4name'>";
echo "<input type='hidden' id='pid' name='pid' value='$field1name'>";
echo" <label for='chkzone'> Product - $field2name </label>";
echo" <label for='chkzone'> :: Zone - $field4name </label>";
//echo" <label for='chkzone'> :: Zone - $field4name </label>";
echo "<a class='btn btn-secondary text-light btn-sm' href='test.php?id=$field4name&pid=$field1name'>Disable</a>";
}
else if($field5name == 0){
echo "<input type='hidden' id='chkzone' name='chkzone' value='$field4name'>";
echo "<input type='hidden' id='pid' name='pid' value='$field1name'>";
echo" <label for='chkzone'> Product - $field2name </label>";
echo" <label for='chkzone'> :: Zone - $field4name </label>";
//echo" <label for='chkzone'> :: Zone - $field4name </label>";
echo "<a class='btn btn-secondary text-light btn-sm' href='test_1.php?id=$field4name&pid=$field1name'>Enable</a>";
}
echo "</td>";
echo "</tr>";
?>
</form>
<?php
}
echo "</table>";
flush();
mysqli_close($conn);
?>
Below codes are from test.php page.
<?php
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();
$zone = $_GET['id'];
$pid = $_GET['pid'];
$updateQuery = "UPDATE productdb.tbl_correlationwafer SET zone_enable='0' WHERE zone='$zone' AND lotid='$pid'";
$result = mysqli_query($conn,$updateQuery);
if($result){
echo "<script>window.history.go(-1);</script>";
//echo "<script>return false;</script>";
}else{
echo "<script>window.history.go(-1);</script>";
//echo "<script>return false;</script>";
}
flush();
mysqli_close($conn);
?>
Below is from test_1.php.
<?php
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();
$zone = $_GET['id'];
$pid = $_GET['pid'];
$updateQuery2 = "UPDATE productdb.tbl_correlationwafer SET zone_enable='1' WHERE zone='$zone' AND lotid='$pid'";
$result = mysqli_query($conn,$updateQuery2);
if($result){
echo "<script>window.history.go(-1);</script>";
//echo "<script>return false;</script>";
}else{
echo "<script>window.history.go(-1);</script>";
//echo "<script>return false;</script>";
}
flush();
mysqli_close($conn);
?>
I'm not an expert so, please, correct me if I'm wrong.
As far as I can see, you're passing query parameters through an <a></a>. As it has an URL specified in the href attribute, it will redirect you right there.
I would definitely use Ajax, as suggested by #CBroe. It will allow you to pass whatever parameters you need for updating your database to the server, without reloading the page. You'll be also able to improve the user experience, and refactor some of the code you've shared.
Take a look at this guide, it helped me a lot to understand how it works.
Good luck!
Hello I have some data in mysql table, like this:
name----cake----image_design
explosion----cheesecake----images/cake/sofia.png
I have all images of the designs in a folder (/cake)
In my php document I get the data:
<?php
session_start();
include "../config/config.php";//Contiene funcion que conecta a la base de datos
if (!isset($_SESSION['user_id'])&& $_SESSION['user_id']==null) {
// header("location: index.php");
}
$id_conect=$_SESSION['user_id'];
$query3=mysqli_query($con, "SELECT * from user where id='$id_conect'");
while ($rs=mysqli_fetch_array($query3))
{
$tipo=$rs['tipo'];
}
$action = (isset($_REQUEST['action']) && $_REQUEST['action'] !=NULL)?$_REQUEST['action']:'';
if (isset($_GET['id'])){
$id_del=intval($_GET['id']);
$query=mysqli_query($con, "SELECT * from pedido where id_pedido='".$id_del."'");
$count=mysqli_num_rows($query);
if ($delete1=mysqli_query($con,"DELETE FROM pedido WHERE id_pedido='".$id_del."'")){
?>
<div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Aviso!</strong> Datos eliminados exitosamente.
</div>
<?php
}else {
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Error!</strong> Lo siento algo ha salido mal intenta nuevamente.
</div>
<?php
} //end else
} //end if
?>
<?php
if($action == 'ajax'){
// escaping, additionally removing everything that could be (html/javascript-) code
$q = mysqli_real_escape_string($con,(strip_tags($_REQUEST['q'], ENT_QUOTES)));
$aColumns = array('cliente');//Columnas de busqueda
$sTable = "pedido";
$sWhere = "";
if ( $_GET['q'] != "" )
{
$sWhere = "WHERE (";
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
$sWhere .= $aColumns[$i]." LIKE '%".$q."%' OR ";
}
$sWhere = substr_replace( $sWhere, "", -3 );
$sWhere .= ')';
}
$sWhere.=" order by created_at desc";
include 'pagination.php'; //include pagination file
//pagination variables
$page = (isset($_REQUEST['page']) && !empty($_REQUEST['page']))?$_REQUEST['page']:1;
$per_page = 10; //how much records you want to show
$adjacents = 4; //gap between pages after number of adjacents
$offset = ($page - 1) * $per_page;
//Count the total number of row in your table*/
$count_query = mysqli_query($con, "SELECT count(*) AS numrows FROM $sTable $sWhere");
$row= mysqli_fetch_array($count_query);
$numrows = $row['numrows'];
$total_pages = ceil($numrows/$per_page);
$reload = './expences.php';
//main query to fetch the data
$sql="SELECT * FROM $sTable $sWhere LIMIT $offset,$per_page";
if($tipo==1|| $tipo==3)
{
$query = mysqli_query($con, $sql);
}else
{
$query=mysqli_query($con, "select * from pedido where id_usuario= $id_conect");
}
//loop through fetched data
if ($numrows>0){
?>
<table class="table table-striped jambo_table bulk_action">
<thead>
<tr class="headings">
<th class="column-title">Cliente </th>
<th>Fecha</th>
<th class="column-title">Tienda </th>
<th class="column-title no-link last"><span class="nobr"></span></th>
</tr>
</thead>
<tbody>
<?php
while ($r=mysqli_fetch_array($query)) {
$id=$r['id_pedido'];
$created_at=date('Y-m-d H:i:s', strtotime($r['created_at']));
// $description=$r['description'];
// $nombre=$r['nombre_ticket'];
// $solucion=$r['solucion'];
// $soporte=$r['soporte'];
// $title=$r['title'];
// $priority_id=$r['priority_id'];
$status_id=$r['id_status'];
// $kind_id=$r['kind_id'];
// $category_id=$r['category_id'];
$Cliente=$r['cliente'];
$project_id=$r["project_id"];
$encargada=$r["nombre_pedido"];
$producto_id=$r["id_producto"];
$cantidad=$r["cantidad"];
$biscocho=$r["biscocho"];
$relleno=$r["id_relleno"];
$design=$r["id_design"];
$opcion=$r["opcion"];
$observacion=$r["observacion"];
$dedicatoria=$r["dedicatoria"];
$c_fondo=$r["color_fondo"];
$c_chongo=$r["color_chongo"];
$c_rosas=$r["color_rosas"];
$figura=$r["figura"];
$c_figura=$r["color_figura"];
$hora=$r["hora"];
$cliente=$r["cliente"];
$telefono=$r["telefono"];
$entrega =$r['fecha_entrega'];
$imagen =$r["id_design"];
$sql = mysqli_query($con, "select * from project where id=$project_id");
if($c=mysqli_fetch_array($sql)) {
$name_project=$c['name'];
}
// $sql = mysqli_query($con, "select * from priority where id=$priority_id");
// if($c=mysqli_fetch_array($sql)) {
// $name_priority=$c['name'];
// }
$sql = mysqli_query($con, "select * from status where id=$status_id");
if($c=mysqli_fetch_array($sql)) {
$name_status=$c['name'];
}
?>
<input type="hidden" value="<?php echo $id;?>" id="id<?php echo $id;?>">
<input type="hidden" value="<?php echo $created_at;?>" id="fecha<?php echo $id;?>">
<input type="hidden" value="<?php echo $project_id;?>" id="project_id<?php echo $id;?>">
<input type="hidden" value="<?php echo $Cliente;?>" id="cliente<?php echo $id;?>">
<input type="hidden" value="<?php echo $encargada;?>" id="encargada<?php echo $id;?>">
<!-- me obtiene los datos -->
<input type="hidden" value="<?php echo $producto_id;?>" id="producto_id<?php echo $id;?>">
<input type="hidden" value="<?php echo $cantidad;?>" id="cantidad<?php echo $id;?>">
<input type="hidden" value="<?php echo $biscocho;?>" id="biscocho<?php echo $id;?>">
<input type="hidden" value="<?php echo $relleno;?>" id="relleno<?php echo $id;?>">
<input type="hidden" value="<?php echo $design;?>" id="design<?php echo $id;?>">
<input type="hidden" value="<?php echo $status_id;?>" id="status_id<?php echo $id;?>">
<input type="hidden" value="<?php echo $opcion;?>" id="opcion<?php echo $id;?>">
<input type="hidden" value="<?php echo $observacion;?>" id="observacion<?php echo $id?>">
<input type="hidden" value="<?php echo $dedicatoria;?>" id="dedicatoria<?php echo $id?>">
<input type="hidden" value="<?php echo $c_fondo;?>" id="c_fondo<?php echo $id?>">
<input type="hidden" value="<?php echo $c_chongo;?>" id="c_chongo<?php echo $id?>">
<input type="hidden" value="<?php echo $c_rosas;?>" id="c_rosas<?php echo $id?>">
<input type="hidden" value="<?php echo $figura;?>" id="figura<?php echo $id ?>">
<input type="hidden" value="<?php echo $entrega;?>" id="entrega<?php echo $id ?>">
<input type="hidden" value="<?php echo $hora;?>" id="hora<?php echo $id ?>">
<input type="hidden" value="<?php echo $cliente;?>" id="cliente<?php echo $id ?>">
<input type="hidden" value="<?php echo $telefono;?>" id="telefono<?php echo $id ?>">
<input type="hidden" value="<?php echo $imagen;?>" id="imagen<?php echo $id ?>">
<tr class="even pointer">
<td><?php echo $cliente;?></td>
<td><?php echo $created_at;?></td>
<td><?php echo $project_id;?></td>
<td ><span class="pull-right">
<i class="glyphicon glyphicon-edit"></i>
</span></td>
</tr>
<?php
} //en while
?>
<tr>
<td colspan=6><span class="pull-right">
<?php echo paginate($reload, $page, $total_pages, $adjacents);?>
</span></td>
</tr>
</table>
</div>
<?php
}else{
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
<strong>Aviso!</strong> No hay datos para mostrar!
</div>
<?php
}
}
?>
Then in another archive.php, I receive the variables like this:
<script>
function obtener_datos(id){
var soporte = $("#soporte"+id).val();
var encargada = $("#encargada"+id).val();
var producto_id = $("#producto_id"+id).val();
var cantidad = $("#cantidad"+id).val();
var biscocho = $("#biscocho"+id).val();
var relleno = $("#relleno"+id).val();
var design = $("#design"+id).val();
var opcion = $("#opcion"+id).val();
var observacion =$("#observacion"+id).val();
var dedicatoria =$("#dedicatoria"+id).val();
var c_fondo = $("#c_fondo"+id).val();
var c_chongo =$("#c_chongo"+id).val();
var c_rosas =$("#c_rosas"+id).val();
var figura =$("#figura"+id).val();
var entrega =$("#entrega"+id).val();
var hora =$("#hora"+id).val();
var cliente =$("#cliente"+id).val();
var telefono =$("#telefono"+id).val();
var kind_id = $("#kind_id"+id).val();
var project_id = $("#project_id"+id).val();
var category_id = $("#category_id"+id).val();
var priority_id = $("#priority_id"+id).val();
var status_id = $("#status_id"+id).val();
var imagen =$("#imagen"+id).val();
$("#mod_id").val(id);
$("#mod_encargada").val(encargada);
$("#mod_producto_id").val(producto_id);
$("#mod_cantidad").val(cantidad);
$("#mod_biscocho").val(biscocho);
$("#mod_relleno").val(relleno);
$("#mod_design").val(design);
$("#mod_opcion").val(opcion);
$("#mod_observacion").val(observacion);
$("#mod_dedicatoria").val(dedicatoria);
$("#mod_c_fondo").val(c_fondo);
$("#mod_c_chongo").val(c_chongo);
$("#mod_c_rosas").val(c_rosas);
$("#mod_figura").val(figura);
$("#mod_entrega").val(entrega);
$("#mod_hora").val(hora);
$("#mod_cliente").val(cliente);
$("#mod_telefono").val(telefono);
$("#mod_soporte").val(soporte);
$("#mod_kind_id").val(kind_id);
$("#mod_project_id").val(project_id);
$("#mod_category_id").val(category_id);
$("#mod_priority_id").val(priority_id);
$("#mod_status_id").val(status_id);
$("#mod_imagen").val(imagen);
}
</script>
Below #mod_name and #mod_cake is the id of text box.
<div class="col-md-9 col-sm-9 col-xs-12">
<input type="text" style="width:150px; float:left;"name="encargada" id="**mod_cake**"class="form-control">
</div>
</div>
</div>
Now my problem is with the image, because return me images/cake/sofia.png and I don't know how I show that image
I try with this:
<img id ="mod_imagen"src="">
but does not work.
i solve that wiht this:
javascript:
var imagen =$("#imagen"+id).val();
document.getElementById("imagenid").src=imagen;
other php:
<img id="imagenid"src="" />
Having the image itself in you database may slow down your queries. Instead, consider storing the image name in the database and the image itself as you usually would: a file in a directory. Then, with Javascript:
document.getElementById("**mod_cake**").src = "<?php echo $filename; ?>";
Im working on a confession website, and on the website I have a section "Top of the Week" which displays 3 most liked confessions from current week, it displays them in a horizontal slider by most liked order, so it goes most liked, second most liked, third most liked confession. The thing that Im trying to do is to put #1, #2, #3 number on each one, but I can't achieve that because I only style one div and website automatically create two other and add confession to it.
This is my php + html code that I use to display them:
<div class="sidebox">
<?php
$select = "SELECT confessions.confessId,
(IFNULL(confessions.firstName, '')) AS firstName,
confessions.confessText,
DATE_FORMAT(confessions.postDate,'%b %d %Y %h:%i %p') AS postDate,
hasImage,
UNIX_TIMESTAMP(confessions.postDate) AS orderDate,
confessions.isActive,
(SELECT COUNT(*) FROM views WHERE views.confessId = confessions.confessId ) as totalViews,
(SELECT COUNT(*) FROM likes WHERE likes.confessId = confessions.confessId ) as totalLikes,
(SELECT COUNT(*) FROM dislikes WHERE dislikes.confessId = confessions.confessId ) as totalDislikes
FROM
confessions
WHERE isActive = 1
ORDER BY totalViews DESC , orderDate DESC limit 3";
$resss = mysqli_query($mysqli, $select) or die('-3' . mysqli_error()); ?>
<div id="sticky-nav" style="height:36px;" class="absolute" style="z-index:0">
<div id="width-limit">
<div class="options">
<ul class="menu">
<li><a class="carousel_prev previous" href="#"><b style="font-size: 20px;">‹</b></a></li>
<li><a style="opacity:1;padding-top:1px;position:relative;left:-54px;"><i class="fas fa-crown" id="crownicon"></i> Top of the Week</a> </li>
<li><div class="menu-clear"></div></li>
<li><a class="carousel_next next" href="#"><b style="font-size: 20px;">›</b></a></li>
</ul>
</div>
<div class="options" id="opt2" style="float:right; width:280px;display:none;">
<ul style="display: inline-block;float:right;">
</ul>
</div><div class="options" id="opt2" style="float:right; width:280px;display:none;">
<ul style="display: inline-block;float:right;">
</ul>
</div>
<div id="small-logo"></div>
<div class="clearfix"></div>
</div>
</div><div class="sidecontainer">
<div class="slick">
<?php
while ($row = mysqli_fetch_assoc($resss)) {
// Get Total Comments
$comssql = "SELECT 'X' FROM comments WHERE confessId = ".$row['confessId']." AND isActive = 1";
$commentstotal = mysqli_query($mysqli, $comssql) or die('-4'.mysqli_error());
$totComments = mysqli_num_rows($commentstotal);
if ($totComments == '1') { $comText = 'Comment'; } else { $comText = 'Comments'; }
if ($row['totalViews'] == '1') { $viewText = 'View'; } else { $viewText = 'Views'; }
$shareURL = $set['installUrl'].'page.php?page=view&confession='.$row['confessId'];
?>
<div class="confession" style="margin-left: 0;width: 300px;">
<div class="left"><span class="label2 label-confess1"><?php echo $row['totalViews'].' '.$viewText; ?></span></div>
<div class="right"><span class="bestthisweek">
<?php if ($row['totalLikes'] == '12') { echo "Top of the Day!"; } else { echo "Top!"; } ?>
</span></div>
<div class="confessionstyle" style="margin-top:20px;"><p>
<font color="#fff3b2">
<?php
if ($filterProfanity == '1') {
echo nl2br(htmlspecialchars(filterwords($row['confessText'])));
} else {
echo nl2br(htmlspecialchars($row['confessText']));
}
?>
</font>
</p></div>
<input type="hidden" id="confessId" name="confessId_<?php echo $count; ?>" value="<?php echo $row['confessId']; ?>" />
<?php
$chkLikes = mysqli_query($mysqli,"SELECT 'X' FROM likes WHERE confessId = ".$row['confessId']." AND likeIp = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
$hasLike = mysqli_num_rows($chkLikes);
$likeCSS = $hasLike > 0 ? 'text-info' : 'white';
$chkDislikes = mysqli_query($mysqli,"SELECT 'X' FROM dislikes WHERE confessId = ".$row['confessId']." AND dislikeIp = '".$_SERVER['REMOTE_ADDR']."' LIMIT 1");
$hasDislike = mysqli_num_rows($chkDislikes);
$dislikeCSS = $hasDislike > 0 ? 'text-danger' : 'white';
?>
<div class="confession-actions">
<div class="likes" style="width: 75px;">
<span class="label2 label-confess first liked">
<a href="" id="likeIt_<?php echo $row['confessId']; ?>" class="likeIt_<?php echo $count; ?> <?php echo $likeCSS; ?>" style="text-decoration:none;outline:none;">
<i class="fas fa-thumbs-up"></i> <span style="color:white;" id="likesVal_<?php echo $row['confessId']; ?>"><?php echo $row['totalLikes']; ?></span>
</a>
</span>
</div>
<div class="dislikes" style="width: 75px;">
<span class="label2 label-confess disliked">
<a href="" id="dislikeIt_<?php echo $row['confessId']; ?>" class="dislike_<?php echo $count; ?> <?php echo $dislikeCSS; ?>" style="text-decoration:none;outline:none;">
<span style="color:white;" id="dislikesVal_<?php echo $row['confessId']; ?>"><?php echo $row['totalDislikes']; ?></span> <i class="fas fa-thumbs-down"></i>
</a>
</span>
</div>
<?php if ($row['hasImage'] != '0') { ?>
<span class="label label-confess"><i class="fa fa-picture-o img"></i></span>
<?php } ?>
<div class="comments">
<div class="divide" style="width: 75px;"><div id="comments-hvr"><a href="page.php?page=view&confession=<?php echo $row['confessId']; ?>">
<i class="fa fa-comments"></i> <?php echo $totComments.' '; ?>
</a></div></div>
</div>
<div class="divide2" style="width: 75px;"><div class="fb-share-button" style="top:-6.5px;transform: scale(0.93);"
data-href="page.php?page=view&confession=<?php echo $row['confessId']; ?>"
data-layout="button_count"></div></div></div>
<div class="clearfix"></div>
</div>
<?php
}
?>
</div>
I tried everything and this is the best solution that I have, but I can't figure out what should I put inside if, elseif, else, I need something that will place #1 on first, #2 on second, #3 on third confession. So I would need something like this but something that will not require me to manually insert > likes.
<?php if ($row['totalLikes'] > '11') { echo "#1"; } elseif ($row['totalLikes'] > '5') { echo "#2"; } else { echo "#3"; }?>
Please visit my website, its still in development, but check Top of the Week on the right and you'll have a clue of what I actually want: http://confessions.byethost31.com
FINAL:
<?php $i = 1; ?>
<?php
echo '#'.$i;
$i++;
?>
Easiest way to to just create a variable that increments with every while iteration.
$i = 1; // set initial value
while ($row = mysqli_fetch_assoc($resss)) {
echo 'This number will grow by 1 for every row - '.$i;
$i++; // this is equal to $i = $i + 1; so will increment by 1 every time.
}
EDIT:
Instead of attempting to assume what the total likes would be, it would be easier to sort them by total likes (putting the most liked first and assigning this #1).
ORDER BY totalLikes DESC, totalViews DESC, orderDate DESC
You can then just use
echo '#'.$i;
Where you want to display the number, you can do this because you have LIMIT 3 so only the top 3 will display.
Objective Inserting name(input-text) and info(textarea-contains multiple lines) into the database, and after submission of the form, at the same page, 2 columns are for displaying the same data in columns, name & info, but under info column. I have made buttons for each row in front of the names, which is used as slideToggle for showing/hiding which contains the data retrieved from the 'info' column
Problem - when I am clicking the button of 1st row, instead of displaying the info related to 1st entry only, it is sliding and showing all info(s) related to all entries at only click.
*others - one more input has been added to the form but as hidden used for id(auto increment)
----index.php-----
<?php include('php_code.php'); ?>
<?php
if (isset($_GET['edit'])) {
$id = $_GET['edit'];
$update = true;
$record = mysqli_query($db, "SELECT * FROM records WHERE id=$id");
if (count($record) == 1 ) {
$n = mysqli_fetch_array($record);
$name = $n['name'];
$acc = $n['acc_no'];
$info = $n['info'];
}
}
?>
<html>
<head>
<title>JSK</title>
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$('form').hide();
$('p').hide();
$('#sp').hide();
$("#inf").click(function(){
$("p").slideToggle();
$('#sp').slideToggle();
});
$("#fo").click(function(){
$("form").slideToggle();
});
});
</script>
</head>
<body>
<div class="container">
<div class="left">
<?php if (isset($_SESSION['message'])): ?>
<div class="msg">
<?php
echo $_SESSION['message'];
unset($_SESSION['message']);
?>
</div>
<?php endif ?>
<?php $results = mysqli_query($db, "SELECT * FROM records"); ?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Account No</th>
<th>Info</th>
<th colspan="2">Action</th>
</tr>
</thead>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<tr>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['acc_no']; ?></td>
<td><button id="inf" onclick="myFunction()">show</button></td>
<td>
<a href="index.php?edit=<?php echo $row['id']; ?>" class="edit_btn" >Edit</a>
</td>
<td>
Delete
</td>
</tr>
<tr id="sp"> <td colspan="4"><p> <?php echo $row['info']; ?> </p></td>
</tr>
<?php } ?>
</table>
<div id="fotable" align="center">
<button id="fo">Add New/Edit</button>
</div>
<form method="post" action="php_code.php" >
<input type="hidden" name="id" value="<?php echo $id; ?>">
<div class="input-group">
<label>Name</label>
<input type="text" autocomplete="off" name="name" value="<?php echo $name; ?>">
</div>
<div class="input-group">
<label>Account No</label>
<input type="text" name="acc" value="<?php echo $acc; ?>">
</div>
<div class="input-group">
<label for="info">Info</label>
<textarea class="form-control" rows="8" name="info" id="info"><?php echo $row['info']; ?></textarea>
</div>
<div class="input-group">
<?php if ($update == true): ?>
<button class="btn" type="submit" name="update" style="background: #556B2F;" >update</button>
<?php else: ?>
<button class="btn" type="submit" name="save" >Add Account</button>
<?php endif ?>
</div>
</form>
</div><!-- left closed -->
<div class="right">
hello
</div> <!-- right closed -->
</div> <!-- container closed -->
</body>
</html>
---php_code.php-----
<?php
session_start();
$db = mysqli_connect('localhost', 'root', '', 'jskrecords');
// initialize variables
$name = "";
$acc = "";
$info = "";
$id = 0;
$update = false;
if (isset($_POST['save'])) {
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "INSERT INTO records (name, acc_no, info) VALUES ('$name', '$acc', '$info')");
$_SESSION['message'] = "Account saved";
header('location: index.php');
}
if (isset($_POST['update'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$acc = $_POST['acc'];
$info = $_POST['info'];
mysqli_query($db, "UPDATE records SET name='$name',acc_no='$acc',info='$info' WHERE id=$id");
$_SESSION['message'] = "Account updated!";
header('location: index.php');
}
if (isset($_GET['del'])) {
$id = $_GET['del'];
mysqli_query($db, "DELETE FROM records WHERE id=$id");
$_SESSION['message'] = "ACC deleted!";
header('location: index.php');
}
?>
Concept:
If you are creating multiple form from same mysql table using php script, You need to give each form a unique id.
e.g.
<form method="post" action="php_code.php" id="form<?= $id ?>">
Then add data-target="#form" to button with class 'inf'. It will store id of form.
<button class="inf" data-target="#form<?= $id ?>">show</button>
When button is clicked we know which form to open from data-target.
<script>
$('.container').on('click','button.inf',function(e){
e.preventDefault();
var formid=$(this).attr('data-target'); //get value of data-target attribute
//...proceed to play toggle with this form 'formid'
My Code is:
<form method="POST" action="" style="margin-top: 30px;">
<div class="form-group" style="display: inline-block;">
<label for="Name" class="label-control" >Device Price</label>
<input type="text" name="price" class="form-control">
</div>
<div class="row" style="margin-bottom: 20px;">
<div class="col-md-3">
<?php
$r = 0;
$sql = "SELECT * FROM Subscribes ";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
if ($r == 5) {
echo "</div><div class='col-md-3'>";
$r = 0;
}
$r++;
$p = $row['price'];
echo "<label style='font-size: 16px; margin: 20px;'>";
echo "<input name='checkbox[]' type='checkbox' ";
echo "value=" . $p . " >" . $row['sub_name'];
if ($p == 0) {
echo "<label style='color:#f00'> Free </label>";
}
echo "</label>";
}
?>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="form-group" style="margin-left: 35%;">
<button class="btn btn-info" name="sum" style="font-size: 24px;">TOTAL</button>
<label style="margin-left: 6%; font-size: 24px;">
<?php
if (isset($_POST['sum'])) {
$price = $_POST['price'];
$price = $price + $price * .52;
$total = $price;
if (!empty($_POST['checkbox'])) {
foreach ($_POST['checkbox'] as $value) {
$total = $total + $value;
}
}
echo $total;
}
?></label>
</div>
</div>
</div>
You can put a checked property to a checkbox:<input type="checkbox" checked>"
In your case, you have to check the POST data in the first block aswell and add the checked property to checkboxes that have been submitted.
echo "<input name='checkbox[]' type='checkbox' ";
echo "value=" . $p;
//if value is in the array of submitted checkboxes, add the checked property
if(array_search($p ,$_POST["checkbox"]) >= 0) echo "checked";
echo ">" . $row['sub_name'];
You can almost just check posted value to see if checked[$r] is on, but in the case where the user leaves some of the boxes unchecked, those don't get posted.
So, you need a way to identify each checkbox. You could do this by naming them like this when you echo them:
$id = $row['id']; // or whatever your id is
echo "<input name='checkbox[$id][]' type='checkbox' value=$p >"
Now, when the user submits the form, you know exactly which checkboxes should be checked. So you can simply add the checked attributed when you render them:
echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>"
So altogether:
<?php
$r = 0;
$sql = "SELECT * FROM Subscribes ";
$result = $db->query($sql);
while ($row = $result->fetch_assoc()) {
if ($r == 5) {
echo "</div><div class='col-md-3'>";
$r = 0;
}
$r++;
$p = $row['price'];
$id = $row['id'];
$checked = null;
if(array_key_exists('checkbox', $_POST) && array_key_exists($id, $_POST['checkbox']){
$checked = "checked";
}
echo "<label style='font-size: 16px; margin: 20px;'>";
echo "<input name='checkbox[$id][]' type='checkbox' value=$p $checked>" . $row['sub_name'];
if ($p == 0) {
echo "<label style='color:#f00'> Free </label>";
}
echo "</label>";
}
?>