Compare MySQL result in PHP - 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"];
}
?>

Related

how to make image as clickable to submit image id to a php file?

I have something like this
while($row = $result->fetch(PDO::FETCH_ASSOC)){
echo '<div class="container">';
$i=0;
while($i<3){
echo '<div class="row">';
echo '<div class="col-sm-4">';
echo '<div class="panel panel-primary">';
echo '<div class="panel-body"><img src="';
echo $row['imageurl'];
echo '"class="img-responsive" style="width:250px" style="height:250px" alt="Image"></div>';
echo '<div class="panel-footer">';
echo $row['name'];
echo '</div></div></div>';
$i++;
}
}
I want to click the image so that it calls a new PHP file with the image id.
To view the output http://csedu.cf/test.php
Try to put your image tag in a tag.
while($i<3)
{
echo '<div class="row">';
echo '<div class="col-sm-4">';
echo '<div class="panel panel-primary">';
echo '<div class="panel-body"><a href="test.php?id=image_id"><img src="';
echo $row['imageurl'];
echo '"class="img-responsive" style="width:250px" style="height:250px" alt="Image"></div>';
echo '</a><div class="panel-footer">';
echo $row['name'];
echo '</div></div></div>';
$i++;
}

Place the data from database to bootstrap

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;
}
}

Cannot retrieve value from sql query

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]);
}

Deleting status post using Ajax POST request

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 ?

jquery accordion not works properly on database search results

I want to load information from data base and show them as accordion.in my case accordian is working only for first set of data retrieves from the database but accordion functionality is not applied for the rest of the data.i'm new to this feature and i added simple accordion function.
$(function() {
$( "#accordion" ).accordion({
collapsible: true,
heightStyle: "content"
});
});
this is the part of php codeigniter code
echo '<div class="fieldinfio"><h4>BRANCHES</h4></div>';
echo '<span class="view_btn">Add Branch</span>';
echo '<div id="branches">';
foreach ($branch_details as $branch) {
echo'<h3>'.$branch->branch_name.'</h3>';
echo '<div id="accordion">';
echo '<h3>Branch Information</h3>';
echo '<div>';
echo '<p>';
echo '<table class="datatable">';
echo '<tr><td width="200">Branch Name: </td>';
echo '<td>'.$branch->branch_name.'</td></tr>';
echo '<tr><td width="200">Account Type: </td>';
echo '<td>'.$branch->account_type.'</td></tr>';
echo '<tr><td width="200">Postcode: </td>';
echo '<td>'.$branch->postcode.'</td></tr></table>';
echo '</p>';
echo '</div>';
echo '<h3>Default Price</h3>';
echo '<div>';
echo '<p>';
if($default_price[$branch->id]) {
} else {
echo '<p>Not Set</p>';
}
echo '</p>';
echo '</div>';
echo '<h3>Training Locations</h3>';
echo '<div>';
echo '<p>';
echo '<span class="view_btn">Add Training Location</span>';
foreach ($location_details[$branch->id] as $location) {
echo '<span>'.$location->name.'</span>';
echo '<table class="datatable">';
echo '<tr><td width="200">Name: </td>';
echo '<td>'.$location->name.'</td></tr>';
echo '<tr><td width="200">Postcode: </td>';
echo '<td>'.$location->postcode.'</td></tr></table>';
}
echo '</p>';
echo '</div>';
echo '</div>';
}
echo '</div>';
It looks like the branch information is inside a loop, which will create multiple elements with the id accordion.
ID of an element must be unique in a document, so in your case use class attribute instead like
echo '<div class="accordion">';
echo '<h3>Branch Information</h3>';
then
$(function () {
$(".accordion").accordion({
collapsible: true,
heightStyle: "content"
});
});

Categories