CSS li inline & jquery sortable - php

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....

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 - How to create 4 columns (4 x 25%) and then end the line with a new column of 4

I have a project going on where we are able to create a product, which is loaded to the front page in PHP.
However, if there are 5 products, then it won't create a new line. What should happen if there are 5 products is 2 lines of 4 each should be displayed.
<section class="ICO">
<div class="one-fourth-gridtable">
<table class="grid table">
<?php
while ($subject = mysqli_fetch_assoc($subject_set)) {
$imagenavn = $subject['navn'];
$sql = "SELECT * FROM product_images WHERE image_name = '$imagenavn'";
$sth = $db->query($sql);
$result = mysqli_fetch_array($sth);
?>
<td>
<div style="border: 1px solid black; border-radius:2000px;">
<img src="data:image/jpeg;base64,<?= base64_encode($result['image']); ?>" />
</div>
<a class="action" href="<?= url_for('../show.php?id=' . h(u($subject['id']))); ?>"><?php echo h($subject['navn']); ?></a>
</td>
<?php } ?>
</table>
</div>
</section>
I tried to style the table with CSS:
table {
background-color: #6991ac;
width: 100%;
text-align: center;
display: inline-table;
margin-top: -5%;
}
.ICO {
max-width: 1100px;
margin: 0 auto;
}
.one-fourth-gridtable {
width: 100%;
}
With this code you get a new line after 4 images and after 8 images the loops stops because of the $u. You can delete/change the $u if not needed.
<?php require_once('private/initialize.php'); ?>
<?php $test = "Alle Produkter"; ?>
<?php $subject_set = find_all_subjects(); ?>
<?php $page_title = 'Forside'; ?>
<?php include(SHARED_PATH . '/staff_header.php'); ?>
<!--- Start of header --><head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="design/Design.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8" />
</head>
<!--- End of header -->
<!--- Start of section (BANNER) -->
<section class="banner">
<div class="banner-inner">
<div id="banner-image"><img src="img/cryptocurrency.png" style="margin-top:10% !important"></div>
</div>
</section>
<!--- End of section (BANNER) -->
<!--- Start of section (ICO'S) -->
<!--- End of section (ICO'S) -->
<div class="sactions">
<!-- <a class="saction" href="<?php echo url_for('/staff/subjects/new.php'); ?>">Create New Subject</a> --> </div>
<section class="ICO">
<div class="one-fourth-gridtable">
<table class="gridtable">
<?php
$i = 0;
$u = 0;
while($subject = mysqli_fetch_assoc($subject_set)) {
$imagenavn = $subject['navn'];
$sql = "SELECT * FROM product_images WHERE image_name = '$imagenavn'";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
$i++;
$u ++;
?>
<td>
<?php
echo '<img src="data:image/jpeg;base64,'.base64_encode( $result['image'] ). '"/> '; ?>
<a class="action" href="<?php echo url_for('../show.php?id=' . h(u($subject['id']))); ?>"><?php echo h($subject['navn']);?></a>
<?php
if ($u > 7 ) {
break;
}
if ($i > 3) {
echo '</tr><tr>';
$i = 0;
}
?>
</td>
<?php } ?>
</table>
</div>
</section>
<?php
mysqli_free_result($subject_set);
?>
Tables automatically expand to contain all of the elements on a single row.
To get around this, you could simply make use of new table rows (<tr>), though I would recommend swapping to a <div> layout and floating the elements to the left:
<div class="contents">
// output the contents
</div>
.contents img {
float: left;
}
This will cause the elements to automatically move to the next line when occupying more than the available width, with no need to worry about how many elements should be displayed per row:
.contents img {
float: left;
margin: 5px;
}
<div class="contents">
<img src="http://placehold.it/200" />
<img src="http://placehold.it/200" />
<img src="http://placehold.it/200" />
<img src="http://placehold.it/200" />
</div>
Hope this helps! :)
First count the results to display, say $count.
<table class="grid table">
<tr>
<?php
$trer = 1;
$cycler = 0;
while($subject = mysqli_fetch_assoc($subject_set))
{
$cycler++;
$imagenavn = $subject['navn'];
$sql = "SELECT * FROM product_images WHERE image_name = '$imagenavn'";
$sth = $db->query($sql);
$result=mysqli_fetch_array($sth);
?>
<td>YOUR CONTENT</td>
<?php
if($trer == 4 && $cycler < $count) //if true you're at the last td of the line, but not on the last line, so let's put a new line
{
echo "</tr><tr>";
$trer = 0;
}
$trer++;
}
if($trer > 0 && $trer < 4) //if line is incomplete (less than 4 items but 1 or more
{
for(int t = 0; $t < 4 - $trer; $t++) //echo empty TDs till filling the line
echo "<td> </td>";
}
?>
</tr>
</table>

PHP Won't echo same code in different div

This part of the code works fine:
<div id="Vraag">
<?php
while($row = mysqli_fetch_assoc($rows))
{
echo $row["vraag"];
}
?>
</div>
But this part does not work:
<div id="antAA">
<?php
while($row = mysqli_fetch_assoc($rows))
{
echo $row["AntwA"];
}
?>
<!-- Antwoord A -->
</div>
And I have no idea why it is not working. It is the same code. The database works fine because if I run this code all the way in the the top of my code it echoes all of them fine:
while($row = mysqli_fetch_assoc($rows))
{
echo $row["id"];
echo $row["vraag"];
echo $row["AntwA"];
echo $row["AntwB"];
echo $row["AntwC"];
echo $row["AntwD"];
}
But in the div it just does not work.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="se.css">
<title>Lotto</title>
</head>
<body>
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD,id FROM vraag1";
$rows = mysqli_query($db, $query);
// while($row = mysqli_fetch_assoc($rows))
// {
// echo $row["id"];
// echo $row["vraag"];
// echo $row["AntwA"];
// echo $row["AntwB"];
// echo $row["AntwC"];
// echo $row["AntwD"];
// }
?>
<div id="headerbg"></div>
<center>
<h1>Vraag 1</h1>
</center>
<center>
<div>
<img src="antw.png" id="img6">
</div>
<div id="Vraag">
<?php
while($row = mysqli_fetch_assoc($rows))
{
echo $row["vraag"];
}
?>
</div>
<div id="plaatje">
<img id="img1" src="wip.jpg">
</div>
<div id="BGantA">
<img id="img2" src="antw.png">
</div>
<div id="antAA">
<?php
while($row = mysqli_fetch_assoc($rows))
{
echo $row["vraag"];
}
?>
<!-- Antwoord A -->
</div>
<div>
<img src="antw.png" id="img3">
</div>
<div>
Antwoord B
</div>
<div>
<img src="antw.png" id="img4">
</div>
<div>
Antwoord C
</div>
<div>
<img src="antw.png" id="img5">
</div>
<div>
Antwoord D
</div>
</center>
</body>
</html>
CSS Code if needed:
body{
background-color: white;
overflow: hidden;
/*#ECECEC;*/
}
#headerbg{
display: block;
margin-top:-10px;
margin-left: -10px;
margin-right: -10px;
width: 102%;
height: 118px;
background-color: #333333;
/*#232323;*/
}
h1{
font-family: times;
display: block;
margin-left: -800px;
margin-top:-76px;
color: #979797;
}
#img2, #img3, #img4, #img5{
width:300px;
height:100px;
display: block;
}
img{
border:1px solid lightgray;
padding: 5px;
}
#img1{
margin-top: 50px;
}
#img2{
margin-top: -370px;
margin-left: -800px;
}
#img3{
margin-top:-67px;
margin-left:800px;
}
#img4{
margin-left:-800px;
margin-top: 150px;
}
#img5{
margin-left: 800px;
margin-top: -67px;
}
#img6{
display: block;
width: 800px;
height: 60px;
margin-top: 55px;
margin-left: ;
}
#antAA, #antBB, #antCC, #antDD{
display: block;
/*color: #979797;*/
color:red;
text-decoration:none;
}
#antAA, #antCC{
margin-top: -65px;
margin-right: 800px;
}
#antBB{
margin-top: -65px;
margin-left: 800px;
}
#antDD{
margin-left: 800px;
margin-top:-65px;
}
#Vraag{
display:block;
color: red;
margin-top: -44px;
}
#antAA:hover, #antBB:hover, #antCC:hover, #antDD:hover{
color:white;
}
Once you execute mysqli_fetch_assoc then it return false as last value has been fetched and has no further value.
If you want to use this for multiple times you can save its value in array and then use this array
<?php
$vraag_array=array();
while($row = mysqli_fetch_assoc($rows))
{
$vraag_array[] = $row["vraag"];
}
//to echo
// close
?>
<div id="Vraag">
<?php
foreach ($vraag_array as $vraag_value) {
echo vraag_value;
}
?>
</div>
<div id="plaatje">
<img id="img1" src="wip.jpg">
</div>
<div id="BGantA">
<img id="img2" src="antw.png">
</div>
<div id="antAA">
<?php
foreach ($vraag_array as $vraag_value) {
echo vraag_value;
}
?>
<!-- Antwoord A -->
</div>
Use:
mysqli_data_seek($rows, 0);
After every while loop.
This sets the counter back to 0
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="se.css">
<title>Lotto</title>
</head>
<body>
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lotto";
$db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD,id FROM vraag1";
$rows = mysqli_query($db, $query);
// while($row = mysqli_fetch_assoc($rows))
// {
// echo $row["id"];
// echo $row["vraag"];
// echo $row["AntwA"];
// echo $row["AntwB"];
// echo $row["AntwC"];
// echo $row["AntwD"];
// }
?>
<div id="headerbg"></div>
<center>
<h1>Vraag 1</h1>
</center>
<center>
<div>
<img src="antw.png" id="img6">
</div>
<div id="Vraag">
<?php
while($row = mysqli_fetch_assoc($rows))
{
echo $row["vraag"];
}
mysqli_data_seek($rows, 0);
?>
</div>
<div id="plaatje">
<img id="img1" src="wip.jpg">
</div>
<div id="BGantA">
<img id="img2" src="antw.png">
</div>
<div id="antAA">
<?php
while($row = mysqli_fetch_assoc($rows))
{
echo $row["vraag"];
}
mysqli_data_seek($rows, 0);
?>
<!-- Antwoord A -->
</div>
<div>
<img src="antw.png" id="img3">
</div>
<div>
Antwoord B
</div>
<div>
<img src="antw.png" id="img4">
</div>
<div>
Antwoord C
</div>
<div>
<img src="antw.png" id="img5">
</div>
<div>
Antwoord D
</div>
</center>
</body>
</html>
It looks like you're iterating through and fetching all the rows, but only reading out the one that matches "vraag". When you execute a second time, you have already iterated through and exhausted all the rows.
That's why this bit works:
while($row = mysqli_fetch_assoc($rows))
{
echo $row["id"];
echo $row["vraag"];
echo $row["AntwA"];
echo $row["AntwB"];
echo $row["AntwC"];
echo $row["AntwD"];
}
Because you're using each row as it is being processed over.
I can't really understand why you're executing such code, which iterates over entire collections and reads out the same singular value each time, but if you want the second attempt to work, you will have to execute this line again:
$rows = mysqli_query($db, $query);
Definitely consider rethinking the entire structure though.
I'm not totally sure. But I think this is because in the first while loop it takes out the "vraag".
So in the second while loop, it may think its the same loop so there is no more data to display there. Try to put in the query again right above the second loop. With other variable names
$query2 = "SELECT id, vraag, AntwA, AntwB, AntwC, AntwD,id FROM vraag1";
$rows2 = mysqli_query($db, $query2);
while($row2 = mysqli_fetch_assoc($rows2))
Please try to use different variables for different values. Please try different variable name in next code which is not working, just like this:
<div id="antAA">
<?php
while($row_new = mysqli_fetch_assoc($rows))
{
echo $row_new["AntwA"];
}
?>
<!-- Antwoord A -->
</div>
I hope, this will solve your issue.

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>

How to apply margin on DIV when printing with loop?

Here is my Code:
<?php
$qr = mysql_query("SELECT * FROM products");
while($res = mysql_fetch_array($qr)):
?>
<div class="box">
<p><?php echo $res[1]; ?></p>
<img src="<?php echo $res[3]; ?>" width="100" /><br />
View Data Sheet
</div>
<?php
endwhile;
?>
I want the output of the fetched record like this http://jsfiddle.net/CqYhE/3/
Please help me that how can I apply margin of 10px on both sides (left & right), only on the center div of each row when printing it with php loop.
Here is your solution:
This will work for N divisions.
<?php
$qr = mysql_query("SELECT * FROM products");
$count = 1;
$margin='';
while($res = mysql_fetch_array($qr)):
$count == 2 ? $margin='style="margin:0px 10px;"' : $margin = '';
$count == 3 ? $count = 1 : $count++;
?>
<div class="box" <?php echo $margin; ?>>
<p><?php echo $res[1]; ?></p>
<img src="<?php echo $res[3]; ?>" width="100" /><br />
View Data Sheet
</div>
<?php
endwhile;
?>
Use css :first-child and :last-child selectors
#container .box{
float: left;
width:245px;
height:200px;
background-color:#0CC;
margin-bottom:10px;
margin-left:10px;
margin-right:10px;
}
#container .box:first-child,
#container .box:last-child { margin-left:0px; margin-right:0px;}
if you have Multiple Rows you will need to change you html to make it easy : See This DEMO
This is best achieved with a CSS solution. If you forget about the left margin and just apply a right one you can then remove it from the last-child. Fewer lines of CSS so better performance.
.box{
float: left;
width:245px;
height:200px;
background-color:#0CC;
margin: 0 20px 10px 0;
}
.box:last-child {
margin-right: 0;
}

Categories