I currently have my page like this, www.page.com/bars.php?monday, www.page.com/bars.php?tuesday, etc. Depending on what day is set, I display different content. Here is how I do it:
if (isset($_GET['monday']) && empty($_GET['monday'])) {
$query = "SELECT * FROM `bars_mon`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['tuesday']) && empty($_GET['tuesday'])) {
$query = "SELECT * FROM `bars_tues`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['wednesday']) && empty($_GET['wednesday'])) {
$query = "SELECT * FROM `bars_wed`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['thursday']) && empty($_GET['thursday'])) {
$query = "SELECT * FROM `bars_thurs`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['friday']) && empty($_GET['friday'])) {
$query = "SELECT * FROM `bars_fri`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['saturday']) && empty($_GET['saturday'])) {
$query = "SELECT * FROM `bars_sat`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else if (isset($_GET['sunday']) && empty($_GET['sunday'])) {
$query = "SELECT * FROM `bars_sun`";
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
}
What I want to know is, when the user goes to www.page.com/bars.php only, how can I make so that I show something like if it were one of the days? And if i select a day, I would like to hide the bars.php content and display only the bars.php?'day' contents. Also, is there any way to make this code shorter? seems like there would be a much shorter way of doing this.
Url should be www.page.com/bars.php?day=tuesday
<?php
$day = (isset($_GET['day']) && !empty($_GET['day']))?$_GET['day']:'';
switch ($day) {
case "monday":
$query = "SELECT * FROM `bars_mon`";
break;
case "tuesday":
$query = "SELECT * FROM `bars_tues`";
break;
case "wednesday":
$query = "SELECT * FROM `bars_wed`";
break;
case "thursday":
$query = "SELECT * FROM `bars_thurs`";
break;
case "friday":
$query = "SELECT * FROM `bars_fri`";
break;
case "saturday":
$query = "SELECT * FROM `bars_sat`";
break;
case "sunday":
$query = "SELECT * FROM `bars_sun`";
break;
default:
$query = false;
}
if($query) {
$result = mysql_query($query) or die("no query");
$objects = array();
while($row = mysql_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
} else {
echo 'your other code here';
}
?>
You need to get keys from $_GET using array_keys.
You will get day as first value.
Steps:
1) Check if $_GET is not empty.
2) Get keys of the array with array_keys.
3) Create an array of allowed week days.
4) Create another multidimensional array of days and database tables.
5) Where key is day and value is database table name.
6) If everything is fine, get name of database table using the day key.
7) This will be database table name.
8) If it is not empty, do the rest of work.
Code:
<?php
$days = isset($_GET) && ! empty($_GET) ? array_keys($_GET) : array();
$day = (current($days)) ? strtolower(current($days)) : '';
$allowedDays = array('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'); // Only allowed days.
if (! in_array($day, $allowedDays)) {
die('incorrect day');
}
$dbTables = array();
$dbTables['monday'] = 'bars_mon';
$dbTables['tuesday'] = 'bars_tues';
$dbTables['wednesday'] = 'bars_wed';
$dbTables['thursday'] = 'bars_thurs';
$dbTables['friday'] = 'bars_fri';
$dbTables['saturday'] = 'bars_sat';
$dbTables['sunday'] = 'bars_sun';
$tableName = isset($dbTables[$day]) ? $dbTables[$day] : '';
if (! empty($tableName)) {
$query = "SELECT * FROM `" . $tableName . "`";
// REST OF THE CODE...
}
?>
Here's a much simpler approach, that allows for a default:
calling with ?day=mon, for example
$days = array(
"mon", "tue", "wed", "thu", "fri", "sat", "sun",
);
$day = "mon";
if (isset($_GET['day']) && in_array($_GET['day'], $days)) {
$day = $_GET['day'];
}
$query = "SELECT * FROM `bars_$day`";
$result = mysqli_query($query) or die("no query");
$objects = array();
while($row = mysqli_fetch_assoc($result)) {
$objects[] = $row;
}
foreach($objects as $object) {
echo
'<div class="bars-loc-cont">
<img src="' . $object['img_link'] . '" />
<div class="bars-info">
<p class="bars-title">' . $object['title'] . '</p>
<p class="bars-line-1">' . $object['line_1'] . '</p>
<p class="bars-line-2">' . $object['line_2'] . '</p>
<p class="bars-line-3">' . $object['line_3'] . '</p>
<p class="bars-line-4">' . $object['line_4'] . '</p>
<p class="bars-line-5">' . $object['line_5'] . '</p>
</div>
</div>';
}
A few tips for future:
Seriously, stop using mysql_* functions - they're deprecated and unsafe. mysqli_* is better, but look into PDO and prepared statements.
Look into better error handling than calling die() - you want nice error messages for users and not raw text
Look into separating data logic (models) from presentation (views)
When each branch in an if/else chain uses the same 15 lines of code and only varies by one value, look at rewriting your code. Now, if you add new information to your output, you only have to update your code once and not seven times.
I think you have to pass monday or any other day like:
www.page.com/bars.php?day=monday
then
you can access like: $_GET['day']
At the beginning of the page, check if your $GET is empty
if (empty($_GET)) {
// no data passed by get, Set your get to your current day or any other day
$current_day = date('l');
$_GET[$current_day] = /**set it to what you want*/;
}
You should go with this:
<?php
# Check if weekday is passed in GET paramater in index `on`
# if not set then get current weekday using PHP date function
# to show current day records instead showing empty page
$day = (isset($_GET['on']) && !empty($_GET['on'])) ? $_GET['on'] : strtolower(date('l'));
$weekdays_and_db_tables = array(
'sunday' => 'sun',
'monday' => 'mon',
'tuesday' => 'tues',
'wednesday' => 'wed',
'thursday' => 'thurs',
'friday' => 'fri',
'saturday' => 'sat',
);
# Now check if weekdays is correct
if (!isset($weekdays_and_db_tables[$day]) or $weekdays_and_db_tables[$day] == '') {
die('Invalid weekday!');
}
# SQL Injection can cause here, so beware
# you should migrate all table in one and filter records using an extra column `weekday`
$query = printf("SELECT * FROM `bars_%s`", $weekdays_and_db_tables[$day]);
# instead separate table for each weekdays migrate in one and do:
$query_x = printf("SELECT * FROM `bars_weekdays` WHERE `weekday` = '%s'", $weekdays_and_db_tables[$day]);
$result = mysqli_query($query) or die("no query");
while($object = mysqli_fetch_assoc($result)) {
echo <<<HTML
<div class="bars-loc-cont">
<img src="{$object['img_link']}" />
<div class="bars-info">
<p class="bars-title">{$object['title']}</p>
<p class="bars-line-1">{$object['line_1']}</p>
<p class="bars-line-2">{$object['line_2']}</p>
<p class="bars-line-3">{$object['line_3']}</p>
<p class="bars-line-4">{$object['line_4']}</p>
<p class="bars-line-5">{$object['line_5']}</p>
</div>
</div>
HTML;
}
Related
I've read about the nl2br() command but they echo out the variable to make it work but my variable is inside a list that I echo out.
Should I somehow put the nl2br() command in my if statement where I'm setting up the output or should I be looking somewhere else for my answer.
I have made 2 extra detail variables to output so I get 3 lines of text for the description of my product but there has to be a better way of doing it than that. I would think 1 detail variable with all the info would be the preferred way of doing it
I have tried to search for it but I'm afraid I'm just not asking the right question so any help in the right direction is appreciated.
<?php require_once './connections/connect_mysqli.php';
$conn = dbConnect('read');
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT 6 ";
$result = $conn->query($sql) or die($conn->error);
$i = 0;
$flatlist = "";
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"];
$details2 = $row["details2"];
$details3 = $row["details3"];
$price = $row["price"];
if ($i % 4 == 0) {
$flatlist .='<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br/>
<p style=font-size:14px;>' . $details . '</p><br />
' . $details2 . '<br />
' . $details3 . '<br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
} else {
$flatlist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br />
<p style=font-size:14px;>' . $details . '</p><br />
' . $details2 . '<br />
' . $details3 . '<br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
}
$i++;
}
$flatlist .= '</tr></table>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Flats</title>
</head>
<body>
<?php require 'includes/skyline.php'; ?>
<?php require 'includes/menu.php'; ?>
<table width="1200" border="0" align="center">
<tr>
<?php echo $flatlist ?>
</tr>
</table>
<?php require 'includes/footer.php';?>
Want to point out you close the details <p> before the 2nd and 3rd lines. You probably want to close that after third line.
<p style=font-size:14px;>' . $details . '<!-- remove here: </p> --><br />
' . $details2 . '<br />
' . $details3 . '<br /><!-- add here: --></p>
If you keep your same database structure, you can define into one variable after retrieving like:
$details = $row["details"] . '<br/>' . $row['details2'] . '<br/>' . $row['details3'];
And then just refer back to $details later on.
Why don't you want to use nl2br()? This would allow you to store multiple lines all into details column. Then you would just use:
$details = nl2br( $row["details"] );`
Here are my edits to your code:
<?php require_once './connections/connect_mysqli.php';
$conn = dbConnect('read');
$sql = "SELECT * FROM products ORDER BY id ASC LIMIT 6 ";
$result = $conn->query($sql) or die($conn->error);
$i = 0;
$flatlist = "";
while($row = $result->fetch_array(MYSQLI_ASSOC)){
$id = $row["id"];
$product_name = $row["product_name"];
$details = $row["details"] . '<br/>' . $row['details2'] . '<br/>' . $row['details3'];
$price = $row["price"];
if ($i % 4 == 0) {
$flatlist .='<tr><td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br/>
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
} else {
$flatlist .= '<td width="250px" align="center"><img src="images/' . $id . '.jpg"><br/>
<p style=color:blue font-size=14px;>' . $product_name . '</p><br />
<p style=font-size:14px;>' . $details . '</p><br />
<p style=font-size:14px;>$' . $price . '</p><br />
<input type="button" value="Order" style=color:blue></td>';
}
$i++;
}
I have a query I'm trying to run in my php code and it is only returning one result, but if i run the same query in phpmyadmin it works. Can anyone tell me where I'm going wrong?
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>
Remove From Cart
</a>
</p>
</div>
</div>
</div>";
}
?>
I have printed the query to make sure the result of the implode is correct and it seems to be as i can run the result in phpmyadmin and it works fine.
Any help would be appreciated.
Try the following (NOT tested as far as I have not sufficent data provided form you):
UPDATED:
<?php
$sql = "SELECT * FROM `product_packs` WHERE `name` IN('" . implode("', '", $_SESSION['cart_items']) . "')";
// dump the query send to the database
$var_dump($sql);
$result = $conn->query($sql);
if ($result->num_rows > 0){
$resultset_count = $result->num_rows;
// dump the number of resultsets in the query
var_dump($resultset_count);
while($row = $result->fetch_assoc()){
echo "<div class='col-xs-6 col-sm-4 col-md-2 col-lg-2'>
<div class='products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='remove_from_cart.php?name=" . $row['name'] . "&price=" . $row['price'] . "'>Remove From Cart </a>
</p>
</div>
</div>
</div>
</div>";
}
?>
Why don't you use FIND_IN_SET? It will work for you, I don't know your database structure but I have created an example query that could be of help for you
why don't use FIND_IN_SET? it will work for you i don't know your database structure still i have created query for you it might help you
SELECT * FROM product_packs
WHERE (
FIND_IN_SET(cart_items, (SELECT cart_items TABLENAME
WHERE cart_items = '$_SESSION['cart_items']')
)
)
ORDER BY `product_id` ASC
Hi I have some products on a website I'm building that I want to be able to filter by the product range that they come from. I have managed to make a filter for specific brands using a drop down menu, but would like to be able to filter the range when clicking on a label/selecting a radio button. I am using php and jquery to try and achieve this, can anybody tell me where I'm going wrong and what I need to do to fix this?
This is my php/html which shows the options of product ranges.
<!-- Product Range -->
<div id="product_range">
<?php
$sql = "SELECT * FROM `product_range`";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// Output data of each rowt
while($row = $result->fetch_assoc()){
//echo "Brand : " . $row["brand"] . " - Product : " . $row["name"] . " - " . $row["description"] . " - Price : " . $row["price"] . "<br>";
echo "<div class='col-lg-6 col-md-4 col-sm-5 col-xs-12 product_range " . $row['brandName'] . " all' id='products'>
<div class='hovereffect'>
<img class='img-responsive product_range_img' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
<form>
<input type='radio' class='radio' name='selector' value='" . $row['product_range'] . "' />
<label for='" . $row['product_range'] . "' id='labelSelect'>
View Products
</label>
</form>
</p>
</div>
</div>
</div>";
}
} else {
echo "0 results";
}
?>
</div>
I then have this part which is the products i wish to display on click.
<!-- Product packs -->
<div id="product_group">
<?php
$sql = "SELECT * FROM `product_packs`";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// Output data of each rowt
while($row = $result->fetch_assoc()){
//echo "Brand : " . $row["brand"] . " - Product : " . $row["name"] . " - " . $row["description"] . " - Price : " . $row["price"] . "<br>";
echo "<div class='col-lg-2.4 col-md-3.4 col-sm-4 col-xs-12 products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
" . $row['header'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='#'>
Add To Cart
</a>
</p>
</div>
<div class='overlay2'>
<div class='overlay3'>
<p>
This is the description
</p>
</div>
</div>
</div>
</div>";
}
} else {
echo "0 results";
}
?>
</div>
<!-- Single products -->
<div id="product">
<?php
$sql = "SELECT * FROM `products`";
$result = $conn->query($sql);
if ($result->num_rows > 0){
// Output data of each rowt
while($row = $result->fetch_assoc()){
//echo "Brand : " . $row["brand"] . " - Product : " . $row["name"] . " - " . $row["description"] . " - Price : " . $row["price"] . "<br>";
echo "<div class='col-lg-2.4 col-md-3.4 col-sm-4 col-xs-12 products " . $row['brandName'] . " all " . $row['product_range'] . "' id='products'>
<div class='hovereffect'>
<img class='img-responsive productimg' src='" . $row['img'] . "' alt=''>
<div class='overlay1'>
<h2> " . $row['name'] . "</h2>
<p>
" . $row['title'] . "
<br>
<br>
" . $row['price'] . "
<br>
<a href='#'>
Add To Cart
</a>
</p>
</div>
<div class='overlay2'>
<div class='overlay3'>
<p>
" . $row['description'] . "
</p>
</div>
</div>
</div>
</div>";
}
} else {
echo "0 results";
}
?>
</div>
this is the jquery i have at the moment, but it only displays the products, not yet filter them.
$(document).ready(function(){
$("#labelSelect").click(function(){
$("#product_range").hide();
$("#product_group").show();
$("#product").show();
$("." + $("input[type='radio'][name='selector']:checked")).show();
$("products:not(." + $("input[type='radio'][name='selector']:checked") + ")").hide();
});
});
Thanks in advance for your help.
I have this code:
$limit = 8;
for($x=0;$x<$limit;$x++) {
$title = substr(str_replace(' & ', ' & ', $feed[$x]['title']), 0, 60);
$link = $feed[$x]['link'];
$description = substr(preg_replace('/\<a.*/', '', $feed[$x]['desc']),0 , 120) . " ... "; //$feed[$x]['desc'];
$image = preg_replace('/\?w.*/', '', $feed[$x]['image']);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
echo '<div class="blog-item item col-md-4' . $rowOpen . '"><img src="' . $image . '"><div class="blog-item-info"><div class="title"><h3>'. $title . '</h3></div><div class="date">' . $date . '</div><div class="preview"><p>'. $description .'</p></div><button class="read-more">Read More</button></div></div></div>';
/*echo '<p><strong>'.$title.'</strong><br />';
echo '<small><em>Posted on '.$date.'</em></small></p>';
echo $image;
//echo '<p>'.$description.'</p>';*/
}
What I want to do is I want every three echos to be wrapped in a
<div class="row"></div>
So I want it to look like this:
<div class="row">
echo '<div class="blog-item item col-md-4' . $rowOpen . '"><img src="' . $image . '"><div class="blog-item-info"><div class="title"><h3>'. $title . '</h3></div><div class="date">' . $date . '</div><div class="preview"><p>'. $description .'</p></div><button class="read-more">Read More</button></div></div></div>';
echo '<div class="blog-item item col-md-4' . $rowOpen . '"><img src="' . $image . '"><div class="blog-item-info"><div class="title"><h3>'. $title . '</h3></div><div class="date">' . $date . '</div><div class="preview"><p>'. $description .'</p></div><button class="read-more">Read More</button></div></div></div>';
echo '<div class="blog-item item col-md-4' . $rowOpen . '"><img src="' . $image . '"><div class="blog-item-info"><div class="title"><h3>'. $title . '</h3></div><div class="date">' . $date . '</div><div class="preview"><p>'. $description .'</p></div><button class="read-more">Read More</button></div></div></div>';
</div><!-- end .row -->
I have tried multiple things but I cannot get it to work. What should I do?
You can check if the $x modulo 3 is 0, then you need a new row. like this:
echo '<div class="row">';
$limit = 8;
for($x=0;$x<$limit;$x++) {
$title = substr(str_replace(' & ', ' & ', $feed[$x]['title']), 0, 60);
$link = $feed[$x]['link'];
$description = substr(preg_replace('/\<a.*/', '', $feed[$x]['desc']),0 , 120) . " ... ";
$image = preg_replace('/\?w.*/', '', $feed[$x]['image']);
$date = date('l F d, Y', strtotime($feed[$x]['date']));
// if x is devidable by 3, start a new row
if($x % 3 == 0 && $x != 0) echo '</div><div class="row">';
echo '<div class="blog-item item col-md-4' . $rowOpen . '"><img src="' . $image . '"><div class="blog-item-info"><div class="title"><h3>'. $title . '</h3></div><div class="date">' . $date . '</div><div class="preview"><p>'. $description .'</p></div><button class="read-more">Read More</button></div></div>';
}
echo '</div>';
note that you are also closing a div to much in your echo ;)
I have this code to pull data from the database and insert the data into one of 4 columns,
I have spent a whole day searching and just cant seem to find out how to do it..
Ideally, I want to select all from database and then where the fetch array has a column id of 1 - echo that then the sama for the next column etc..
<?php
echo '<div class="column grid_3 clearfix" id="column0" >';
echo ' ';
$user_sites_0=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='0' ORDER BY sort_no");
if(!$user_sites_0) {
echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
}
else {
while($user_site_0=mysqli_fetch_array($user_sites_0))
{
$id = stripslashes($user_site_0['id']);
$site_name = stripslashes($user_site_0['site_name']);
$site_address = stripslashes($user_site_0['site_address']);
$site_desc = stripslashes($user_site_0['site_desc']);
$site_category = stripslashes($user_site_0['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_0['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
}
echo '</div>';
?>
<?php
echo '<div class="column grid_3 clearfix" id="column1" >';
echo ' ';
$user_sites_1=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='1' ORDER BY sort_no");
if(!$user_sites_1) {
echo '';
}
else {
while($user_site_1=mysqli_fetch_array($user_sites_1))
{
$id = stripslashes($user_site_1['id']);
$site_name = stripslashes($user_site_1['site_name']);
$site_address = stripslashes($user_site_1['site_address']);
$site_desc = stripslashes($user_site_1['site_desc']);
$site_category = stripslashes($user_site_1['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_1['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
}
echo '</div>';
?>
<?php
echo '<div class="column grid_3 clearfix" id="column2">';
echo ' ';
$user_sites_2=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='2' ORDER BY sort_no");
if(!$user_sites_2) {
echo '';
}
else {
while($user_site_2=mysqli_fetch_array($user_sites_2))
{
$id = stripslashes($user_site_2['id']);
$site_name = stripslashes($user_site_2['site_name']);
$site_address = stripslashes($user_site_2['site_address']);
$site_desc = stripslashes($user_site_2['site_desc']);
$site_category = stripslashes($user_site_2['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_2['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
}
echo '</div>';
?>
<?php
echo '<div class="column grid_3 clearfix" id="column3">';
echo ' ';
$user_sites_3=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='3' ORDER BY sort_no");
while($user_site_3=mysqli_fetch_array($user_sites_3))
{
$id = stripslashes($user_site_3['id']);
$site_name = stripslashes($user_site_3['site_name']);
$site_address = stripslashes($user_site_3['site_address']);
$site_desc = stripslashes($user_site_3['site_desc']);
$site_category = stripslashes($user_site_3['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
echo '<div class="dragbox" id="item'.$id.'">';
echo '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
echo '<div class="dragbox-content" ';
if($user_site_3['collapsed']==1)
echo 'style="display:none;" ';
echo '>';
echo '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
echo '<p>' . $site_category . '</p>';
echo '<p>' . $site_address . '</p>';
echo '<p>' . $site_desc . '</p>';
echo' </div>
</div>';
}
echo '</div>';
?>
The code looks a right state.. Could I do it better?
Use for and change the code, some code :
<?php
for ($i=0; $i<4; $i++)
{
echo '<div class="column grid_3 clearfix" id="column'.$i.'" >';
echo ' ';
$user_sites=mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='".$i."' ORDER BY sort_no");
if(!$user_sites) {
echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
}
else
{
while($user_site=mysqli_fetch_array($user_sites))
{
... // Do it yourself
maybe
function get_site($column_id){
$str = "";
$user_sites = mysqli_query($connection, "SELECT * FROM user_sites WHERE column_id='".$column_id."' ORDER BY sort_no");
if(!$user_sites_0) {
echo 'No sites added, please <a class="addsite" href="#">add one now</a>';
}
else {
while($user_site_0=mysqli_fetch_array($user_sites_0))
{
$id = stripslashes($user_site_0['id']);
$site_name = stripslashes($user_site_0['site_name']);
$site_address = stripslashes($user_site_0['site_address']);
$site_desc = stripslashes($user_site_0['site_desc']);
$site_category = stripslashes($user_site_0['site_category']);
$getImage = 'http://immediatenet.com/t/s?Size=1024x768&URL='.$site_address;
$str.= '<div class="dragbox" id="item'.$id.'">';
$str.= '<h2 class="h2handle">'.$site_name.' <span class="close"><img src="assets/img/closepanel.png"></span></h2>';
$str.= '<div class="dragbox-content" ';
if($user_site_0['collapsed']==1)
echo 'style="display:none;" ';
$str.= '>';
$str.= '<p><a class="sitelink" href="' . $site_address . '" title="'.$site_name.'"><img src="'.$getImage.'" alt="'.$site_name.'" title="'.$site_name.'"/></a>';
$str.= '<p>' . $site_category . '</p>';
$str.= '<p>' . $site_address . '</p>';
$str.= '<p>' . $site_desc . '</p>';
$str.=' </div>
</div>';
}
}
$str.='</div>';
return $str;}
echo '<div class="column grid_3 clearfix" id="column0">';
echo ' ';
echo oget_site(0);
echo '<div class="column grid_3 clearfix" id="column1">';
echo ' ';
echo oget_site(1);
//....