PHP - Header Redirect when data is fetched - php

I want my page to automatically redirect to another page when the data is fetch on the page so how can I do it ? i have this code
here is my code here is where the data is fetched but once it fetch i want to redirect it automatically.
<div>
<?php
$data = mysqli_query($con,"SELECT * FROM announcement");
$count = mysqli_num_rows($data);
if ($count != 0) {
//IVE ASSUMED THAT THIS WILL WORK BUT IT DIDNT
header("location:../addRedirect.php");
while($row = mysqli_fetch_array($data)) {
echo '<div id="myModal" class="modal" style="position:fixed; display: none; padding-top: 100px;
left: 0; top: 0; width: 100%; height: 100%; overflow: auto; background-color: rgb(0,0,0); background-color: rgba(0,0,0,0.9); ">
<div class="container" style="width: 100%">
<div class="card col-12" style="background-color: red; color: white;">
<div class="card-header">
<p><h3 class="text-center">Announcement</h3></p>
</div>
<div class="card-body text-center">
<h5>'.$row['additional_info'].'</h5>
<p>Please click refresh button to resume after the announcement</p>
</div>
</div>
</div>
<img class="modal-content" id="img01">
</div>';
$dataid = $row['id'];
}
}
?>
</div>
and on my second page i want to redirect it back.. so what i have is an auto refresh when im just fetching data.. thanks guide me pls this is a huge help for me as a starter of php and codings thanks.
<?php
header("location:../home.php");
?>

You can refresh your page using javascript instead of PHP, if the headers are already set.
<div>
<?php
$data = mysqli_query($con,"SELECT * FROM announcement");
$count = mysqli_num_rows($data);
if ($count != 0) {
echo "<script>setTimeout(\"location.reload()\",1000);</script>";
// Other stuff ...
}
?>
</div>
If you want to navigate to an other page you can do this:
<div>
<?php
$data = mysqli_query($con,"SELECT * FROM announcement");
$count = mysqli_num_rows($data);
if ($count != 0) {
echo "<script>setTimeout(\"location.href = '../addRedirect.php';\",1000);</script>";
// Other stuff ...
}
?>
</div>
Both examples have a delay of 1 second (1000 milliseconds)

Related

How to display 2 images per slide in carousel using php while loop?

I have a problem with displaying images in carousel. I want to display 2 images per carousel slide. I took images from database using while loop to create slides. The problem is that with my code it only displays one image per slide.
This is how it looks now:
Check the image
<?php
$brojacPoStrani = 0;
$sqlIzvestaji = mysqli_query($con, "SELECT operacije.nazivEng, izvestaji.operacija, izvestaji.ucinak, izvestaji.id FROM izvestaji INNER JOIN operacije ON izvestaji.operacijaId=operacije.id WHERE projekatId='$projekatId' AND datum='$datum'");
while ($row = mysqli_fetch_array($sqlIzvestaji)) {
$id = $row['id'];
$sqlSlike = mysqli_query($con, "SELECT img_name FROM slike WHERE izvestajId='$id' AND datum='$datum'");
$brojacDuplikata = false;
while ($row2 = mysqli_fetch_array($sqlSlike)) {
if ($brojacPoStrani % 2 == 0) {
?>
<div class="carousel-cell" style="background-image: url('img/izvestaji.jpg'); background-repeat: no-repeat; background-size: 100% 350px;">
<h1 style="color: #fff; text-align: left; padding-left: 15px; font-weight: bold;"><?php echo $row['nazivEng']; ?></h1>
<?php
if ($brojacDuplikata === false) {
$brojacDuplikata = true;
?>
<p style="text-align: left; padding-left: 45px; padding-top: 20px; padding-bottom: 10px;"><?php echo $row['operacija'] . " - " . $row['ucinak']; ?></p>
<?php
} else {
?>
<p style="text-align: left; padding-left: 45px; padding-top: 40px; padding-bottom: 10px;"></p>
<?php
}
?>
<div class="row">
<?php
if ($brojacPoStrani % 2 == 0) {
?>
<div class="col-lg-6">
<img src="../files/izvestaji/<?php echo $project; ?>/<?php echo $datum; ?>/<?php echo $row['nazivEng']; ?>/<?php echo $row2['img_name']; ?>" style="width: 350px; height: 350px; padding-left: 10px;" class="float-right" />
</div>
<?php
} else {
?>
<div class="col-lg-6">
<img src="../files/izvestaji/<?php echo $project; ?>/<?php echo $datum; ?>/<?php echo $row['nazivEng']; ?>/<?php echo $row2['img_name']; ?>" style="width: 350px; height: 350px; padding-right: 10px;" class="float-left" />
</div>
<?php
}
?>
</div>
<br />
</div>
<?php
}
$brojacPoStrani++;
}
$brojacPoStrani = 0;
}
As mentioned it looks like you should be able to use a single query rather than having nested queries - and to display 2 images per carousel slide you effectively want to select data from the current and next rows. One easy way to do that would be to assign the entire recordset to a variable and then process that array using a for loop. The following is a simplified, semi-pseudo code version that attempts to combine the sql queries and assign recordset to an array. It is not tested as such but it might? be of use.
$sql="select
o.naziveng,
i.operacija,
i.ucinak,
i.id,
s.img_name
from izvestaji i
inner join operacije o on i.operacijaid=o.id
inner join slike s on s.izvestajId=i.id
where projekatid='$projekatid' and datum='$datum'";
$res = mysqli_query( $con, $sql );
if( $res ){
$arr = mysqli_fetch_all( $res, MYSQLI_BOTH );
for( $i=0; $i < count( $arr ); $i+=2 ){
try{
$r1=array_key_exists( $i+0, $arr ) ? $arr[ $i+0 ] : false;
$r2=array_key_exists( $i+1, $arr ) ? $arr[ $i+1 ] : false;
/*
generate the HTML structure and add two images
*/
echo '<div class="carousel-cell">'; # simplified version
if( $r1 )echo 'row 1: '.$r1['img_name'];
if( $r2 )echo 'row 2: '.$r2['img_name'];
echo '</div>';
}catch( Exception $e ){
continue;
}
}
}

php pick data from web and show it in div

i have a div and also on same page i have some data which is show on web page i just want data show on div how can i do this? i am very very new in php .thanks here is the php file.
<?php
define('HOST','xxxxxxxxxxx');
define('USER','xxxxxxxxxxxxx');
define('PASS','xxxxxxxx');
define('DB','androidapi2');
$con = mysqli_connect(HOST,USER,PASS,DB);
$sql = "select * from users WHERE status = 1";
$res = mysqli_query($con,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array(
// 'id'=>$row[0],
'email'=>$row[3],
));
}
echo json_encode(array("result"=>$result));
mysqli_close($con);
?>
<html>
<head>
<title>DIC ChatBox Beta 1</title>
<style>
#usersOnLine {
font-family:tahoma;
font-size:12px;
color:black;
border: 3px teal solid;
height: 525px;
width: 250px;
float: right;
overflow-y:scroll;
}
.container{
width:970px;
height:auto;
margin:0 auto;
}
</style>
</head>
<body>
<div class="container">
</div>
<h2> all contacts</h2>
<div id="usersOnLine">
</div>
</div>
</body>
</html>
here is the div id usersonline where i want all data show . how to made it.
There is no need to use json_encode(array("result"=>$result));, directly use $result
<div id="usersOnLine">
<?php
foreach($result as $key => $val)
{
echo $val['email'];
echo "<br>";
}
?>
</div>
If you want to use it on another page
Creating New Session In php file
<?php
session_start(); ///at the top of this file
///after your query and creating array
$_SESSION["result"]= $result;////set $result in session
?>
Getting Session on another page
<?php
session_start();
if(isset($_SESSION["result"]))
{
$result = $_SESSION["result"];
///your foreach loop to print data
}
?>
READ SESSION DOCUMENTAION

datatables with ajax in php

I am working on event management site where i've to send emails and show the read, unread, bounced back...etc status email address in modal box. Showing the email addresses in modal box is working fine. I want to add datatables to it. When i Load the modal box first time it is showing the pagination but when i click on second time there is datatables error. Can Anyone help me in this please? Below is my code:-
$("body").on("click", ".textcontent", function(){
var option = $(this).attr("option");
var campaign_id = $("#campaign").val();
$.ajax({
type: "POST",
url: "get_emaillist.php",
data: { option : option, campaign_id : campaign_id },
success: function (resp){
$("#emailslist").html(resp);
$("#myModal").modal("show");
}
});
});
Below is the response from ajax
<?php
$option = $_REQUEST["option"];
$campaign_id = $_REQUEST["campaign_id"];
$emaillist = $gnrl->emaillist($campaign_id,$option);
$totalemail = count($emaillist);
?>
<div class="container">
<div class="row">
<div class="agent-profile">
<div class="col-lg-10 col-sm-10">
<div class="modal fade modelbx" id="myModal" role="dialog">
<div class="modal-dialog">
<div class="modal-content" style="padding: 20px;">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title"><?php echo $option." Users List"; ?></h4>
<?php if($totalemail){ ?>
Export Users
<?php } ?>
</div>
<div class="modal-body">
<?php if(isset($campaign_id) && $campaign_id != "" && $campaign_id != "0"){ ?>
<div class="form-group">
<label class="control-label" for="selectbasic">Event Name</label><hr>
<div><span class="emaillist"><?php echo $campaigntitle = $dclass->select("tbl_campaign","campaignsubject","id = :id",array("id" => $campaign_id)); ?></span></div>
</div>
<?php } if($option == "Nominated" && $totalemail > 0){ ?>
<div class="form-group">
<table border="1" id="statstable">
<thead>
<tr>
<th>Nominated By User</th>
<th>Nominated User Name</th>
<th>Email</th>
<th>Company</th>
<th>Contact Number</th>
<th>Mobile Number</th>
</tr>
</thead>
<tbody>
<?php
if(isset($campaign_id) && $campaign_id != "0"){
$nominateduid = $dclass->select("tbl_nominateusers n left join tbl_user u on n.user_id = u.id","n.id,n.user_id,u.emailaddress"," AND n.campaign_id = '$campaign_id'");
}else{
$nominateduid = $dclass->select("tbl_nominateusers n left join tbl_user u on n.user_id = u.id","n.id,n.user_id,u.emailaddress");
}
foreach($nominateduid as $value){
$nominid = $value["id"];
if(isset($campaign_id) && $campaign_id != "0"){
$selcond = " AND campaign_id = '$campaign_id' AND id = '$nominid'";
}else{
$selcond = " AND user_id = '{$value["user_id"]}' AND id = '$nominid'";
}
$emaildet = $dclass->select("tbl_nominateusers","*",$selcond);
?>
<tr>
<td>
<?php
if($value["user_id"] == 0){
if(isset($campaign_id) && $campaign_id != "0"){
$nominatedemailaddress = $dclass->select("tbl_emailusers","useremail","campaign_id = :campaign_id AND user_id = :user_id",array("campaign_id" => $campaign_id, "user_id" => 0));
}else{
$nominatedemailaddress = $dclass->select("tbl_emailusers","useremail","user_id = :user_id",array("user_id" => 0));
}
}else{
$nominatedemailaddress = $value["emailaddress"];
}
echo $nominatedemailaddress;
?>
</td>
<td>
<?php
if(isset($emaildet[0]["nominatename"]) && $emaildet[0]["nominatename"] != ""){
echo $emaildet[0]["nominatename"];
}else{
echo "---";
}
?>
</td>
<td>
<?php
if($totalemail > 0){
echo $emaildet[0]["useremails"];
}else{
echo "There is No $option Email";
}
?>
</td>
<td>
<?php
if(isset($emaildet[0]["companyname"]) && $emaildet[0]["companyname"] != ""){
echo $emaildet[0]["companyname"];
}else{
echo "---";
}
?>
</td>
<td>
<?php
if(isset($emaildet[0]["contactnumber"]) && $emaildet[0]["contactnumber"] != ""){
echo $emaildet[0]["contactnumber"];
}else{
echo "---";
}
?>
</td>
<td>
<?php
if(isset($emaildet[0]["mobilenumber"]) && $emaildet[0]["mobilenumber"] != ""){
echo $emaildet[0]["mobilenumber"];
}else{
echo "---";
}
?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<?php }else{ ?>
<div class="form-group">
<table border="1" id="statstable">
<thead><tr><th>Email Address</th></tr></thead>
<tbody>
<?php
if($totalemail > 0){
foreach($emaillist as $emails){
?>
<tr><td><?php echo $emails; ?></td></tr>
<?php } }else{ ?>
<tr><td><?php echo "There is No $option Email"; ?></td></tr>
<?php } ?>
</tbody>
</table>
</div>
<?php } ?>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<link rel="stylesheet" href="../css/bootstrap.min.css">
<link rel="stylesheet" href="../css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="../js/bootstrap.min.js"></script>
<style>
.modal.fade.in{ top: 10%!important; }
img{ max-width: 125%!important; }
.modal-dialog{ width: 900px!important; }
.modal .modal-body{ max-height: 420px; overflow-y: auto; }
.modal{ width: auto!important; }
.carttable, .carttable td{ padding: 5px; }
.modelbx{ background: rgba(0, 0, 0, 0) url("../images/login-main-bg.png") repeat scroll 0 0; bottom: 0; display: none; left: 0; position: fixed; right: 0; top: 0; z-index: 999999 !important; }
.sbbtn{ background-color: green; border: 0!important; border-radius: 10px; color: #fff; height: 35px; width: 90px; }
.alert-success{ color: #3c763d; background-color: #dff0d8; border-color: #d6e9c6; }
.alert{ padding: 15px; margin-bottom: 20px; border: 1px solid transparent; border-radius: 4px; }
.textcontent{ float: left; width:100%; color: #5C9BD5; }
.emaillist{ color: #5698D4; line-height: 30px; margin: 10px; }
td, th{ padding: 10px!important; }
#statstable{ margin-bottom: 20px!important; }
</style>
<script type="text/javascript">
$(document).ready(function (){
$("#statstable").dataTable({
"aaSorting": [[0,"desc"]]
});
});
</script>
When you charge new elements with ajax, the javascript code doesn´t know those new elements. Maybe you can try recalling the data-tables init in the success section of the ajax call.

Equals returns all the values in php

In this code I tried to filter some data records from the database using CategoryId but it returns all the category Id's. In my database Category Id's are INT field.
If I echo $data['Category']; then it shows all the categoryId's But after the variable comparison it not work.
I mean if($cat == 1) is Not working(It's not filter/Show data)
//Some coding here
<section id="section-1">
<div class="vs-content">
<div class="widgets-container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
$sql = "SELECT Id,Title,description,Image,Category from News ";
$query = mysql_query($sql);
?>
<div class="blocks">
<?php while($data = mysql_fetch_array($query)){
$cat = $data['Category'];
if($cat == 1){
?>
<h3 style="margin-left: 10px;"><?php echo $data['Title'] ?></h3>
<div style="width: 50%; margin-right: auto; margin-left: auto;">
<img src="images/drink2.jpg" /></div>
<img src="data:image/jpeg;base64,<?php base64_encode( $data['Image'] )?>"/>
<p style="color: white; font-size: 0.6em;"><?php echo $data['Description'] ?></p>
<?php
}
}
?>
</div>
<div class="blocks">
<?php while($data = mysql_fetch_array($query)){
$cat = $data['Category'];
if($cat == 2){
?>
<h3 style="margin-left: 10px;"><?php echo $data['Title'] ?></h3>
<div style="width: 50%; margin-right: auto; margin-left: auto;">
<img src="images/drink2.jpg" /></div>
<img src="data:image/jpeg;base64,<?php base64_encode( $data['Image'] )?>"/>
<p style="color: white; font-size: 0.6em;"><?php echo $data['Description'] ?></p>
<?php
}
}
?>
</div>
<div class="blocks">
<?php while($data = mysql_fetch_array($query)){
$cat = $data['Category'];
if($cat == 3){
?>
<h3 style="margin-left: 10px;"><?php echo $data['Title'] ?></h3>
<div style="width: 50%; margin-right: auto; margin-left: auto;">
<img src="images/drink2.jpg" /></div>
<img src="data:image/jpeg;base64,<?php base64_encode( $data['Image'] )?>"/> -->
<p style="color: white; font-size: 0.6em;"><?php echo $data['Description'] ?></p>
<?php
}
}
?>
</div>
</div>
</div>
// Some coding here
Why not filter the records in your database query? This would mean changing your database query as follows:
$sql = "SELECT Id,Title,description,Image,Category from News WHERE Category = 1";
By taking this approach less data will be returned to PHP and the database call will execute a little faster :)
Update: You only really need the three different <div class="blocks"> sections if the contents within them is going to be different. Otherwise you could simply use a single while loop and output the content for all categories in sequence using the data as ordered by the SQL statement.
//Some coding here
<section id="section-1">
<div class="vs-content">
<div class="widgets-container">
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mydb") or die(mysql_error());
$sql = "SELECT Id,Title,description,Image,Category from News ORDER BY Category";
$query = mysql_query($sql);
?>
<div class="blocks">
<?php while($data = mysql_fetch_array($query)){
?>
<h3 style="margin-left: 10px;"><?php echo $data['Title'] ?></h3>
<div style="width: 50%; margin-right: auto; margin-left: auto;">
<img src="images/drink2.jpg" /></div>
<img src="data:image/jpeg;base64,<?php base64_encode( $data['Image'] )?>"/>
<p style="color: white; font-size: 0.6em;"><?php echo $data['Description'] ?></p>
<?php
}
?>
</div>
</div>
</div>
</section>
// Some coding here
If you only need data rendered for a single category then use the WHERE clause in SQL (See first example) to filter the data you require to reduce the SQL call and improve performance.
Why cant you check the condition in select query, Try this,
$sql = "SELECT Id,Title,description,Image,Category from News where Category='1'";
Update:
<div class="blocks">
<?php
while($data = mysql_fetch_array($query)){
$cat = $data['Category'];
if($cat == 1){ ?>
//Your code
<?php }else if($cat == 2){ ?>
// Your code
<?php }else{ ?>
<?php }?>
<?php }?>
</div>

CSS li inline & jquery sortable

having an issue with the jquery sortable code - it's not so much the jquery but how my list of images is showing. Prior to adding the sortable code it displayed the images in my wrapper in a grid like formation perfectly - now I have added a little style from the jquery it seems to have thrown it but im not sure why...
here is the Jquery style:
<style>
#sortable { list-style-type: none; margin: 0; padding: 0; width: 450px; }
#sortable li {float: left; }
</style>
Here is my list of images that worked perfectly ok before:
<div id="images">
<hr style="margin-top: 10px" />
<ul id="sortable">
<?php
// get folio for user
if(isset($_GET['userid'])) {
$query = 'SELECT * FROM folio WHERE userid=\'' . $_GET['userid'] . '\' ORDER BY ord';
}
else {
$query = 'SELECT * FROM folio WHERE userid=\'' . $_SESSION['userid'] . '\' ORDER BY ord';
}
$result = mysqli_query($connection, $query);
if(!$result) {
// error with MYSQL
die('Failed to retrieve images! - ' . mysqli_error($connection));
}
// list images
$numRows = mysqli_num_rows($result);
if($numRows < 1) {
echo '';
}
else {
$filepath = 'uploads/folio/';
while($imagerow = mysqli_fetch_assoc($result)) {
?>
<li>
<div class="outimgbox"> <a class="fancybox fancybox.ajax" rel="gallery1" style="text-decoration: none;" data-fancybox-type="ajax" href="profile_image_fancybox.php?imgid=<?php echo $imagerow['imgid']; ?>">
<div id="mainwrapper">
<div id="box-3" class="box"> <img class="uploads" src="<?php echo $filepath . $imagerow['filename']; ?>" alt="<?php echo $imagerow['description']; ?>"/> <span class="caption fade-caption">
<h3 ><?php echo $imagerow['title']; ?></h3>
</span> </div>
</div>
</a> </div>
</li>
<!-- class outingbox -->
<?php }} ?>
</ul>
For some odd reason:
<style>
#sortable { list-style-type: none; }
#sortable li {float: left; }
</style>
This resolved the issue....

Categories