I want to get data from the database and write them to the following bootstrap structure: 1 row - 3 columns, 1 row - 3 columns, 1 row - 3 columns.
The following bootstrap structure
<div class="row">
<div class="col-sm-2">
some content
</div>
<div class="col-sm-2">
some content
</div>
<div class="col-sm-2">
some content
</div>
</div>
I just can not figure out how to construct a cycle. Stuck on this place.
while($row = mysqli_fetch_assoc($result)) {
$counter ++;
if($counter == 1 || $counter % 3 === 0) {
echo '<div class="row">';
}
echo '<div class="col-sm-2">';
echo '<div class="row">';
echo '<p>' . $row["name"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["email"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["comment"] . '</p>';
echo '</div>';
echo '</div>';
if($counter % 3 === 0) {
echo '</div>';
}
}
//$counter should be set out here to 1...
//Start here with row don't have to worry about 1
echo '<div class="row">';
while($row = mysqli_fetch_assoc($result)){
//Other echo stuff...
//Check if 3 yet
if($counter == 3) {
//reset to 0 cause you will incrment
$counter = 0;
//end and start new div
echo '</div>';
echo '<div class="row">';
}
$counter ++;
}
//Close it up unless it is always ending on 3?
//Probably want some more checking here somehow
echo '</div>';
$counter = 0;
while($row = mysqli_fetch_assoc($result)) {
if($counter % 3 === 0) {
echo '<div class="row">';
}
echo '<div class="col-sm-2">';
echo '<div class="row">';
echo '<p>' . $row["name"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["email"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["comment"] . '</p>';
echo '</div>';
echo '</div>';
if($counter % 3 === 2) {
echo '</div>';
}
$counter++;
}
//if last row has less then 3 items then close the outer row
if($counter % 3 !== 0) {
echo '</div>';
}
JSfiddle with simulated output for 7 items
This task led to the discovery that before composing algorithms in the code, I need to draw a scheme on a piece of paper. Than test the output on a piece of paper. As a result, I found such a simple solution.
$counter = 0;
while($row = mysqli_fetch_assoc($result)) {
$counter ++;
if($counter == 1) {
echo '<div class="row">';
}
echo '<div class="col-sm-2">';
echo '<div class="row">';
echo '<p>' . $row["name"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["email"] . '</p>';
echo '</div>';
echo '<div class="row">';
echo '<p>' . $row["comment"] . '</p>';
echo '</div>';
echo '</div>';
if($counter == 3) {
echo '</div>';
$counter = 0;
}
}
Related
When I'm using SQL select statements with prepared statements code works fine and display content when I'm run that in my computer localhost using WAMP server.
But when I'm upload this code to my web hosting. No any result display or no any error display.But without prepared statement code works fine in the web hosting and display results. here is my codes with and without prepared statements. Please tell me why that happens?
Code with prepared statements.
<?php
for($i=0;$i <$count; $i++){
require('connection.php');
$stmt = $connection->prepare("SELECT * FROM comnt WHERE status = 'Approved' limit 1 offset ?");
$stmt->bind_param('s', $id);
$stmt->execute();
$result = $stmt->get_result();
if($result->num_rows > 0) {
while($row = $result-> fetch_assoc()){
$pst_content = $row['content'];
$author = $row['name'];
if($i==0){
echo '<div class="item active">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}else{
echo '<div class="item">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}
}
}
}
?>
Code without prepared statements.
<?php
for($i=0;$i <$count; $i++){
require('connection.php');
$qry = "SELECT * FROM comnt WHERE status = 'Approved' limit 1 offset $i";
$select_cmnt = mysqli_query($connection,$qry);
while($row = mysqli_fetch_assoc($select_cmnt)){
$pst_content = $row['content'];
$author = $row['name'];
if($i==0){
echo '<div class="item active">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}else{
echo '<div class="item">';
echo ' <blockquote>';
echo '<div class="row">';
echo '<div class="col-sm-12">';
echo " <p style='color:#a07936'>$pst_content</p>";
echo "<small>$author</small>";
echo ' </div>';
echo ' </div>';
echo ' </blockquote>';
echo ' </div>';
}
}
}
?>
My query not return any value.
$minprice = itemDetails("SELECT MIN(extra_night) FROM hotel_room WHERE hotel_id ='".$row[hotel_id]."'");
Below is my site
https://www.borneoecotours.com/hotels/index--.php
<?php
//$sql_query = $querystring;
$j=0;
if(mysqli_num_rows(mysqli_query($dbhandler,$querystring)) > 0){ //if there is a record
$sql_result = mysqli_query($dbhandler,$querystring)or die(mysqli_error());
while($row = mysqli_fetch_array($sql_result)){
echo '<div class="summbox">';
echo '<div class="summheader">';
echo '<div class="summtitle"><a href="view.php?id='.$row[hotel_id].'" class="hotlink">'.$row[code].' : '.stripslashes($row[name]).' ';
for ($x=1; $x<=$row[star]; $x++){echo "<span class='icon-star blue'></span>";}
echo '</a></div>';
echo '<div class="summduration">'.$row[location].', '.ucwords($row[region]).'</div>';
echo '</div>';
echo '<br clear="right" />';
echo '<div class="summcenter">';
if ($row[pic]==""){
echo '<div class="summpic"><img src="image/'.$row[hotel_id].'/profile.jpg" width="200" height="150" /></div>';
}else{
echo '<div class="summpic"><img src="../image/tours/'.$row[pic].'" width="200" height="150" /></div>';
}
echo '<div class="summdetails">'.stripslashes($row[brief]).'</div>';
$minprice = itemDetails("SELECT MIN(extra_night) FROM hotel_room WHERE hotel_id ='".$row[hotel_id]."'");
echo '<div class="summprice" style="width:250px;">RATES FROM <br />MYR <span style="font-size:20px;">'.$minprice.'</span></div>';
echo '<br clear="right" />';
echo '<div class="summfooter">';
echo 'Details '; //details
if ($row[hotel_id] != '17')
echo 'Book Now'; //enquiry
else
echo 'Book Now'; //enquiry
//echo '<div class="summbutton_03"></div>'; //booknow
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="clear">';
}
} else{
echo '<div class="alert-msg error">No record. Please search again.</div>';
}
?>
Supposed to be the $minprice will return the value form my database. Any idea?
The output should shown MYR (value from database),Example: MYR 1200
function itemDetails($query){
$sql_query = mysql_query($query);
$item = mysql_fetch_array($sql_query);
return stripslashes($item[0]);
}
Im creating a basic website that will show 10 different tv programmes.
I have the 10 different programmes stored in the database. Im able to retriev the 10 programmes but they all appear in a column.
I was wondering if theres a way to have then appear 5 in a row?
I have tried basic CSS but i cant seem to get it working
Here is the code i have so far:
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
while($obj = $results->fetch_object())
{
echo '<br>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
}
}
?>
I was wondering if theres a way to achive that i want or will i have to try something else?
anything will help.
Thanks!
Try to put them in a table:
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
$i=0;
echo '<table><tr>';
while($obj = $results->fetch_object())
{
echo '<td>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
echo '</td>';
$i++;
if ($i == 5) {
echo '</tr><tr>';
}
}
echo '</tr></table>';
}
?>
You can start from:
$i=0;
echo '<br>';
while($obj = $results->fetch_object())
{
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
if (($i++) == 5) { echo '<br>'; $i=0; }
}
UPDATE CSS
.tvProgs {
float:left;
width:200px;
display:block;
}
This will put them in a table 5 in each row just like you asked for.
<?php
$results = $mysqli->query("SELECT * FROM programmes ORDER BY ProgrammeName ASC");
if ($results) {
$i = 0;
echo '<table>';
while($obj = $results->fetch_object())
{
if ($i == 0) {
echo '<tr>';
}
echo '<td>';
echo '<div class="tvProgs">';
echo '<form method="post" id = "books" action="cart_update.php">';
echo '<div class="progImage"><img src="images/'.$obj->Image.'"></div>';
echo '<div class="progTitle"><h3>'.$obj->ProgrammeName.'</h3>';
echo '</form>';
echo '</div>';
echo '</tr>';
$i++;
if ($i == 5) {
echo '</tr>';
$i = 0;
}
}
if ($i != 0) {
echo '</tr>';
}
echo '</table>';
}
?>
how do i delete a single record in database? i tried with Ajax and it didn't work.here is my code,each row has a unique number ID,so i need that to delet it,but i don't know how to get it.
echo '<tr>';
echo '<td>';
echo '<a href="korisnik.php?id='.$session_poster_id.'">';
echo '<div class="profile_pic_div" style="margin-top:10px;">';
echo "<img src='$avatar' style='width:40px;height:40px;'>";
echo '</div>';
echo '</a>';
echo '<div class="timestamp" style="margin-left:50px;margin-top:-43px;font-size:15px;">';
echo $user['vrijeme'];
echo '</div>';
echo '<a href="delete.php?post_id='.$post_id.'">';
echo '<div class="icon-x" style="margin-left:550px;margin-top:-15px;">';
echo '</div>';
echo '</a>';
echo '<div class="post_div" style="margin-top:30px;">';
if (strlen($user['post']) > 500) {
$user['post'] = substr($user['post'], 0, 500);
$user['post'] = substr($user['post'], 0, strrpos($user['post'], ' ')).'... Read More';}
echo $user['post'];
echo '</div>';
echo '<br>';
echo 'Komentiraj';
echo '</td>';
echo '</tr>';
DELETE FROM Name_of_your_table
WHERE Columname = your_value;
see w3schools delete query
PS. What is in your delete.php ?
I had retrieve some result from database and want to display the result separately.
If the pb_title is same as temp_title then put it togather in a red color div
else is pb_title is not same as temp_title then put it in grey color div
Here my tried code :
$select_brand = "SELECT * FROM tblProduct_Brand WHERE pb_display='display'";
$result = mysqli_query ($mydatabase, $select_brand);
if($result)
{
while($row = mysqli_fetch_array($result))
{
$pb_feature_image = substr(($row['pb_feature_image']),3);
$pb_logo = substr(($row['pb_logo']),3);
$temp_result;
if($temp_result == $row["pb_title"])
{
//if product brand title is same, put it in same area
echo '<div style="padding:20px; background-color:red;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"];
}
else
{
//if not same, put in another area
echo '<div style="padding:20px; background-color:grey;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
}
}
Any mistake about the code please correct me.
Thanks.
The following code assumes that you did something like this with the data you fetched from the sql query:
$data = array();
while($row = mysqli_fetch_assoc($result)) {
$data[] = $row;
}
Which returns an array like this:
Array (
[0] => Array(
'pb_title' => '',
'pb_content' => '',
//...etc
),
//.....etc
)
Now assuming that I understand your question correctly, you'd want to do something like this:
$i = array();
$key = 'A';
foreach ($data as $item) {
if ($item['pb_title'] == $key) {
$i['red'][] = $item;
} else {
$i['grey'][] = $item;
}
}
Which gives you an array of red (Matching) and grey (Not Matching).
Which you can loop through and generate your div's appropriately.
Something like this (pseudo code)
echo '<div style="padding:20px; background-color:red;">';
foreach($i['red'] as $row) {
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
}
echo '</div>';
Example
$temp_result;
if($temp_result == $row["pb_title"])
Before checking the condition you should have assigned some value to $temp_result , but you haven't and you are comparing it anyway.
EDIT :
What I understand is that initially you have not set any value of $temp_result and it is always coming in your Else Block where you do'not assign $temp_result = $row["pb_title"]; , So that it shoudl store the previous value , you only did it in the If Block , you need to place the same code in your else block too..
Simple Edit this section in your code
else
{
//if not same, put in another area
echo '<div style="padding:20px; background-color:grey;">';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"]; // added this to save previous result
}
Make sure that programing should be effective for that you need to have following ideas:
1) Here you no need to use if($result) because when the sql results are true then only it will allow inside the condition
2) While programing try to avoid more if else condition for making efficient.
<?php
$select_brand = "SELECT * FROM tblProduct_Brand WHERE pb_display='display'";
$result = mysqli_query ($mydatabase, $select_brand);
while($row = mysqli_fetch_array($result))
{
$pb_feature_image = substr(($row['pb_feature_image']),3);
$pb_logo = substr(($row['pb_logo']),3);
$temp_result;
if($temp_result == $row["pb_title"]) { $style = 'style="padding:20px; background-color:red;"'; }
else { $style = ' style="padding:20px; background-color:grey;"'; }
echo '<div '.$style.' >';
echo '<div style="display:inline-block;width:340px;height:250px;vertical-align:top;">';
echo '<img src="'.$pb_feature_image.'" width="100%" height="100%"/>';
echo '</div>';
echo '<div style="display:inline-block;width:330px;height:250px;padding-left:10px;">';
echo '<div style="height:40px;width:80px;float:right">';
echo '<img src="'.$pb_logo.'" width="100%"/>';
echo '</div>';
echo '<div style="clear:both"></div>';
echo '<a class="product-content">'.$row["pb_content"].'</a>';
echo '</div>';
echo '</div>';
$temp_result = $row["pb_title"];
}
?>