I want to conditionally display a div so when there are no values, the div is not displayed.
Without the php if part, all is working fine, the css styles are applying to the divs.
But when i add the if condition, the CSS doesn't apply anymore.
Here is the div part:
<?php
$getCar_1 = getCar_1();
$getCar_2 = getCar_2();
if(!empty($getCar_1) || !empty($getCar_2)){
?>
<div class="block">
<div class="up">
<div class="title"><h4>Cars</h4></div>
</div>
<div class="down">
<div class="column_1"><b><?php getCar_1(); ?></b></div>
<div class="column_2"><b><?php getCar_2(); ?></b></div>
</div>
</div>
<?php
}
?>
and here is the CSS part:
.block{
width:900px;
margin:0 auto;
overflow:hidden;
float:left;
}
.up{
width:100%;
text-align:center;
border:1px solid;
margin-top:10px;
margin-bottom:10px;
background-color:skyblue;
}
.down{
width:100%;
clear:both;
}
.column_1{
float:left;
width:47%;
padding:10px;
}
.column_2{
float:right;
width:47%;
text-align:right;
padding:10px;
}
I know this code is very simple but i can't understand why it is not working properly.
To be more specific.
If i use only this code:
<div class="block">
<div class="up">
<div class="title"><h4>Cars</h4></div>
</div>
<div class="down">
<div class="column_1"><b><?php getCar_1(); ?></b></div>
<div class="column_2"><b><?php getCar_2(); ?></b></div>
</div>
</div>
it display normal:
using only div
And when adding the php if part it display this:
ading php if
So when i add the php if, the css has no effect anymore. I also tried the inline styling variant like <div class="block" style="......"> but it won't work this way either.
EDIT: I'm back because i think the issue comes from the functions get getCar_1(); and getCar_2(); which are in functions.php page and look like this:
<?php
function getCar_2(){
global $connect;
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());
while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
$car_model_2 = $row_car_2['car_name'];
$car_image_2 = $row_car_2['car_image'];
if(isset($car_model_2) && isset($car_image_2)){
echo "
<div id='' style='white-space:nowrap;'>
<p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a>
<img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
</div>";
}
}
}
?>
Instead of:
if(!empty($getCar_1) || !empty($getCar_2)){
?>
Try this:
if((!empty($getCar_1) || (!empty($getCar_2)): ?>
and instead of:
<?php
}
?>
Try this:
<?php endif; ?>
You are not returning anything from getCar_2() function. What value you expect to be in $getCar_2 by line : $getCar_2 = getCar_2(); same question about getCar_1() function if its same like getCar_2() with no return statement in it.
Problem is that your if block will never execute because your condition:
if((!empty($getCar_1) || (!empty($getCar_2)):
will never be true as $getCar_1 and $getCar_2 variables will always be empty since getCar_1() and getCar_2() are not returning anything.
So only echo you are providing from the function getCar_2() and getCar_1() is being printed as output to HTML and your if block is not executing at all.
You should put return statement in getCar_2() and getCar_1() functions if you want if block to work.Like this:
Instead of:
<?php
function getCar_2(){
global $connect;
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());
while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
$car_model_2 = $row_car_2['car_name'];
$car_image_2 = $row_car_2['car_image'];
if(isset($car_model_2) && isset($car_image_2)){
echo "
<div id='' style='white-space:nowrap;'>
<p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a>
<img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
</div>";
}
}
}
?>
Use this:
<?php
function getCar_2(){
global $connect;
$response = NULL;
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());
while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
$car_model_2 = $row_car_2['car_name'];
$car_image_2 = $row_car_2['car_image'];
if(isset($car_model_2) && isset($car_image_2)){
$response = 'success';
echo "
<div id='' style='white-space:nowrap;'>
<p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a>
<img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
</div>";
}
}
return $response;
}
?>
I hope it helps
To me seems to be something wrong where you are printing the data, it should be:
<?php
$getCar_1 = getCar_1();
$getCar_2 = getCar_2();
if(!empty($getCar_1) || !empty($getCar_2)){
?>
<div class="block">
<div class="up">
<div class="title"><h4>Cars</h4></div>
</div>
<div class="down">
<div class="column_1"><b><?php echo $getCar_1; ?></b></div>
<div class="column_2"><b><?php echo $getCar_2; ?></b></div>
</div>
</div>
<?php
}
?>
EDIT: The issue was solved with the help of an admin from another forum. As Banzay said it would have been good if i would post the get getCar_1(); and getCar_2(); functions earlier.
The issue was with the functions. Anyway if other beginners like me will have this issue here is the solution.
I modified this:
<?php
function getCar_2(){
global $connect;
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());
while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
$car_model_2 = $row_car_2['car_name'];
$car_image_2 = $row_car_2['car_image'];
if(isset($car_model_2) && isset($car_image_2)){
echo "
<div id='' style='white-space:nowrap;'>
<p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a>
<img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
</div>";
}
}
}
?>
into this:
<?php
function getCar_2(){
global $connect;
$text = "";
$get_car_2 = "SELECT * FROM cars WHERE category_id=5";
$get_car_result_2 = mysqli_query($connect, $get_car_2) or die(mysqli_query());
while($row_car_2 = mysqli_fetch_array($get_car_result_2)){
$car_model_2 = $row_car_2['car_name'];
$car_image_2 = $row_car_2['car_image'];
if(isset($car_model_2) && isset($car_image_2)){
$text = "
<div id='' style='white-space:nowrap;'>
<p id='model' style='display:inline-block; margin-right:10px; margin-left:10px; padding-top:10px; position:relative; bottom:35px;'>$car_model_2</p></a>
<img src='administrator/images/$car_image_2' width='120' height='80' style='display: inline-block; box-shadow: 0 0 11px #000;'/><br>
</div>";
}
}
return $text;
}
?>
I replaced echo "..." with $text = "..." and returned the variable after the while closing bracket, like this: return $text;
In order to work the $text variable must be initiated before the query like this: $text = "";
Related
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;
}
}
}
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.
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>
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....
my site which is a search engine returns many many results with a foreach loop as such:
foreach ($xml->channel->item as $result) {
$ltitle = $result->title;
$ldesc = $result->description;
$url = $result->displayUrl;
$link = $result->link;
if (strlen($ltitle) > 60)
{
$title = substr($ltitle,0,60).'...' ;
}
else
{
$title = $ltitle;
}
if (strlen($ldesc) > 195)
{
$desc = substr($ldesc,0,195).'...' ;
}
else
{
$desc = $ldesc;
}
echo "
<br>
<div class='resultbox'>
<a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='$link'>$title</a><br>
<div style='padding-top:3px;padding-bottom:4px;width:580px;'>
<font style='text-decoration:none;font-size:small;font-family:Arial;'>$desc<br></font></div>
<a style='text-decoration:none;' href='$link'><font style='text-decoration:none;font-size:small;color:green;font-weight:bold;'>$url<br></font></a>
</div>
";
}
And the resultbox class above styles all of the results with this
.resultbox
{
height:auto;
width:600px;
background-color:transparent;
font-size:19px;
padding:10px;
padding-left: 30px;
padding-right: 30px;
border-left: 6px solid #333;
}
.resultbox:hover
{
border-left: 8px solid #555;
}
The border-left color is what i want changed, i would like it to generate or to style randomly off of a list of colour codes so the results, insead of being all #333 can be #333 #555 #999 and so on..... any ideas?
If u have no problems using JS , You can certainly do this :
$(document).ready(function () {
$('.resultbox').mouseenter(function() {
var randomColor = Math.floor(Math.random()*16777215).toString(16);
$('.resultbox').css("border-left", " 8px solid #"+randomColor);
});
});
change <div class='resultbox'> to <div class='resultbox random-color-".rand(1,YOUR_COLOR_LIMIT)."'> AND define colors like
.random-color-1 {
border-left: 8px solid #555;
}
.random-color-2 {
border-left: 8px solid #555;
}
.....
.random-color-YOUR_COLOR_LIMIT {
border-left: 8px solid #555;
}
change
<div class='resultbox'>
to
<div class='resultbox' style='border-left-color:$yourColorInCssFormat;'>
the style attribute overrides the css from class.
set $yourColorInCssFormat to the color you wish to have for the div. for example: $yourColorInCssFormat = '#999';
You can use inline style for that. Or alternatively you can user nth-child selector of css to repeat the border-color scheme something like this:
.resultbox:nth-child(n+1):hover {
}
.resultbox:nth-child(2n+1):hover {
}
.resultbox:nth-child(3n+1):hover {
}
First off, try this out for your foreachloop:
<?php foreach ($xml->channel->item as $result): ?>
<?php
$ltitle = $result->title;
$ldesc = $result->description;
$url = $result->displayUrl;
$link = $result->link;
if (strlen($ltitle) > 60){
$title = substr($ltitle,0,60).'...' ;
}else{$title = $ltitle;}
if (strlen($ldesc) > 195){
$desc = substr($ldesc,0,195).'...' ;
}else{$desc = $ldesc;}
?>
<div class='resultbox'>
<a class='normal' style='text-decoration:none;font-size:huge;font-weight:bold' href='<?php echo $link ?>'><?php echo $title; ?></a>
<br>
<div style='padding-top:3px;padding-bottom:4px;width:580px;'>
<font style='text-decoration:none;font-size:small;font-family:Arial;'>
<?php echo $desc; ?><br>
</font>
</div>
<a style='text-decoration:none;' href='<?php echo $link; ?>'><font style='text- decoration:none;font-size:small;color:green;font-weight:bold;'><?php echo $url; ?><br></font> </a>
<?php endforeach; ?>
That way you're not playing with big echos.
Now for generating random colors your could use php rand();
For example:
//Generate a random number between the two parameters
$randomNumber = rand(1, 3);
//Use this number to dictate what the variable color should be
if($randomNumber == 1){$color = "#333"}
elseif($randomNumber == 2){$color = "#555"}
elseif($randomNumber == 3){$color = "#999"}
You can then use the variable $color in your code to randomly assign one of the colors to elements.
Hope this helps!
-Gui