Group all the message by username like Facebook does - php

Okay, After few hours of coding I have end up with this code and am stuck here. What I' am trying to do is, if you goto your Facebook message you will see that Facebook groups all the messages from username and displays on the page, well thats exactly what am trying to do over here.
Below is the code I have in place. Still trying to get something to work for me. Also screen shot of the current code example.
SQL
<?php
$session_id = GET_SESSION_ID_VALUE(ENCRYPTION_KEY);
$sql = "SELECT messages.*, profile.profile_id, profile.profile_name, profile.profile_photo, profile.profile_username FROM messages INNER JOIN profile ON profile.profile_id = messages.message_from WHERE messages.message_from = ' " . $session_id . "' OR messages.message_to = ' " . $session_id . "' ORDER BY messages.message_datetime DESC";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$message_id = $row->message_id;
$message_from = $row->message_from;
$message_content = $row->message_content;
$message_content = (strlen($message_content) > 90) ? substr($message_content, 0, 100) . '...' : $message_content;
$message_username = $row->profile_username;
$message_name = $row->profile_name;
$message_photo = $row->profile_photo;
/* HTML GOES HERE */
}
}
?>
Database table
Screenshot of the above code.

<?php
$session_id = GET_SESSION_ID_VALUE(ENCRYPTION_KEY);
$sql = "SELECT messages.*, profile.profile_id, profile.profile_name, profile.profile_photo, profile.profile_username FROM messages INNER JOIN profile ON profile.profile_id = messages.message_from WHERE messages.message_from = ' " . $session_id . "' OR messages.message_to = ' " . $session_id . "' ORDER BY messages.message_datetime DESC";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$prevMessage_from=""; --Get name of previous message
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
while ($prevMessage_from=$row->message_from)
{--Here must be code that add messages to existing--}
if ($prevMessage_from<>$row->message_from)
{
$message_id = $row->message_id;
$message_from = $row->message_from;
$message_content = $row->message_content;
$message_content = (strlen($message_content) > 90) ? substr($message_content, 0, 100) . '...' : $message_content;
$message_username = $row->profile_username;
$message_name = $row->profile_name;
$message_photo = $row->profile_photo;
$prevMessage_from=$message_from;
/* HTML GOES HERE */
}
}
}
?>

Related

How to properly optimise mysql select statement

I have a web page that is divided into different sections. Each section has to show different results. These results are gotten from the database.
This is a sample data on SQLfiddle
http://sqlfiddle.com/#!9/ad98b/1
The following code is what comes to my mind but I'm afraid that it might somehow overload the server when this page is accessed multiple times by different people
$sectionA = $connect->query("SELECT * FROM Main_Section WHERE section = `A`
");
while ($row = $sectionA->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_a = $sec_result_a.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionB = $connect->query("SELECT * FROM Main_Section WHERE section = `B` ");
while ($row = $sectionB->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_b = $sec_result_b.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
$sectionC = $connect->query("SELECT * FROM Main_Section WHERE section = `C` ");
while ($row = $sectionC->fetch_array(MYSQLI_BOTH))
{
$id = $row["id"];
$name = $row["name"];
$sec_result_c= $sec_result_c.'<p>'.$id.'</p><h3>'.$name.'</h3>';
}
UP TO section Z
Is there a way I can Optimise this properly?
Unless there's more to the picture, why not just query everything, ordered by section, to have the A-Z:
SELECT * FROM Main_Section ORDER BY section
... and then process the results with one loop, which could look something like this:
$sections = $connect->query("SELECT * FROM Main_Section ORDER BY section");
while ($row = $sections->fetch_array())
{
echo $row['section'] . ' ' . '<p>' . $row['id'] . '</p><h3>' . $row['firstname'] . ' ' . $row['lastname'] . '</h3>';
}

How do i change the URL id?

Hi how do i change the URL id if the id is above max id in the db?
If i get the id from a database and use it in a read more button to make people read the data in a new page with the id as different url query?
--The real question
How do i make sure that if there only are 4 news in the db that if you write etc
newsTest.php?id=5 in the browser the browser will not execute or go back to max page?
//read more button
Læs mere
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages >= $thisVar){
echo "alt er godt";
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;
$result = $dbCon->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_object();
$id = $row->id;
$heading = utf8_encode($row->heading);
$subheading = utf8_encode($row->subheading);
$description = utf8_encode($row->description);
$created = $row->created;
$author = utf8_encode($row->author);
$output .= $id . "<br>" . $heading . "<br>" . $description;
};
// udskriv output til bruger
echo $output;
};
} else {
$_GET['id'] = $total_pages ;
echo "nothing";
}
?>
I am very new to php
Check if total_pages is less than id then show show total_pages id.
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
if(isset($_GET['id']) && !empty($_GET['id'])){
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages < $thisVar){
$thisVar=$total_pages
$id = $thisVar;
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;

PHP: Loop looping through result set

I am having a huge issue looping through results, These two queries work hand in hand to check if a restaurant is open today. My problem is i have restaurants, id 1-5(more in the future). But the loop seems to only get restaurant id 5. I have read many posts on here and it seems like i am doing the right thing. But i cannot seem to loop to get the other restaurant id's.
I am blocked now, newbie who is very open to any suggestions or advise.
$sel = "SELECT Rest_Details.Resturant_ID,Delivery_Pcode.Pcode,Delivery_Pcode.Restaurant_ID
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE'$searchP'";
$res = $dbc->query($sel);
if (!$res) {
echo "invalid query '" . mysqli_error($dbc) . "\n";
}
$i=1;
while ($row_res = $res->fetch_array()) {
$rest_ = $row_res['Resturant_ID'];
$i++;
}
date_default_timezone_set("Europe/London");
$daynum = jddayofweek(unixtojd());
$query = "SELECT *
FROM Opening_hrs WHERE
Restaurant_ID = $rest_
AND Day_of_week = $daynum";
$run_qu = $dbc->query($query);
if ($run_qu->num_rows > 0) {
while ($row_qu = $run_qu->fetch_assoc()) {
$message = "open" . $row_qu["Open_time"] . "</br>";
}
} else {
$message = $message . "close" . $row_qu["Closing_time"] . "</br>";
}
You could either output whatever you want to within your loop or build-up an output string because the value of $rest_ will always be the last value in the loop and i don't think that's what you want... Again you are doing the same with $message. And I am willing to bet that this is what you want to do:
<?php
date_default_timezone_set("Europe/London");
$sel = "SELECT Rest_Details.Resturant_ID,Delivery_Pcode.Pcode,Delivery_Pcode.Restaurant_ID
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID=Rest_Details.Resturant_ID
WHERE Delivery_Pcode.Pcode LIKE'$searchP'";
$res = $dbc->query($sel);
if (!$res) {
echo "invalid query '" . mysqli_error($dbc) . "\n";
}
$i=1;
while ($row_res = $res->fetch_array()) {
$rest_ = $row_res['Resturant_ID'];
$i++; // <== YOU DON'T NEED THIS VARIABLE....
// GET THE DATES WITHIN THE LOOP...
$daynum = jddayofweek(unixtojd());
$query = "SELECT *
FROM Opening_hrs WHERE
Restaurant_ID = $rest_
AND Day_of_week = $daynum";
$run_qu = $dbc->query($query);
if ($run_qu->num_rows > 0) {
while ($row_qu = $run_qu->fetch_assoc()) {
$message = "open" . $row_qu["Open_time"] . "</br>";
}
} else {
$message = $message . "close" . $row_qu["Closing_time"] . "</br>";
}
}
I think this is what you are trying to do.
// $searchP should be checked to prevent SQL injection.
$sel = "SELECT Rest_Details.Resturant_ID, Delivery_Pcode.Pcode,
Delivery_Pcode.Restaurant_ID
FROM Rest_Details INNER JOIN Delivery_Pcode
ON Delivery_Pcode.Restaurant_ID = Rest_Details.Resturant_IDW
WHERE Delivery_Pcode.Pcode LIKE '$searchP'";
$res = $dbc->query($sel);
if (!$res) {
echo "invalid query '" . mysqli_error($dbc) . "\n";
}
// set these once as they don't change
date_default_timezone_set("Europe/London");
$daynum = jddayofweek(unixtojd());
// $i=1; - not required, never used
// loop over the original results
while ($row_res = $res->fetch_array()) {
$rest_ = $row_res['Resturant_ID'];
//$i++; not used
// check for a match
$query = "SELECT * FROM Opening_hrs
WHERE Restaurant_ID = $rest_
AND Day_of_week = $daynum";
$run_qu = $dbc->query($query);
if ($run_qu->num_rows > 0) {
// at least one match
while ($row_qu = $run_qu->fetch_assoc()) {
$message = "open" . $row_qu["Open_time"] . "<br />";
$message .= "close" . $row_qu["Closing_time"] . "<br />";
}
} else {
// no matches
$message = "No results for <i>$daynum</i>.";
}
}
It should be possible to get the details in a single query, but I would need to see your SQL tables for that (and you did not ask for that too :]).
Also, it is <br> or <br />, not </br>.

PHP - Fatal error: Call to a member function super_query() on a non-object

Fatal error: Call to a member function super_query() on a non-object in /mp3.is-great.org/htdocs/modules/profile.php on line 7
This is my profile.php:
<?php
if(isset($_REQUEST['username'])) $username = $_REQUEST['username'];
if(!$username) die("User not found!");
$user = $db->super_query("SELECT username, user_id, lastdate, reg_date, banned, info, foto, fullname, playlist, song FROM tan_users WHERE username = '$username'");
if(!$user) die("User not found!");
$row = $db->super_query("SELECT COUNT(*) AS count FROM tan_favorites WHERE user_id = '" . $user['user_id'] . "'");
$user['favorites'] = $row['count'];
$row = $db->super_query("SELECT COUNT(*) AS count FROM tan_playlists WHERE user_id = '" . $user['user_id'] . "'");
$user['playlists'] = $row['count'];
$smarty->assign("User", $user);
$action = $_REQUEST['action'];
$smarty->assign("Action", $action);
if($action == 'favorites'){
$db->query("SELECT source, hash, name FROM tan_favorites WHERE user_id = '" . $user['user_id'] . "'");
while ($row = $db->get_row($sql_result)){
$row['play_url'] = play_url($row['hash'], $row['source'], $row['name']);
$favorites[] = $row;
}
$smarty->assign('Favorites', $favorites);
}elseif($action == 'playlists'){
$db->query("SELECT tan_playlists.id, tan_playlists.foto, tan_playlists.name, tan_users.username, tan_users.fullname FROM tan_playlists LEFT JOIN tan_users ON tan_playlists.user_id = tan_users.user_id WHERE tan_playlists.user_id = '" . $user['user_id'] . "'");
while ($row = $db->get_row($sql_result)){
$playlists[] = $row;
}
$smarty->assign('Playlists', $playlists);
}else{
$db->query("SELECT source, hash, name FROM tan_favorites WHERE user_id = '" . $user['user_id'] . "' LIMIT 0,10");
while ($row = $db->get_row($sql_result)){
$row['play_url'] = play_url($row['hash'], $row['source'], $row['name']);
$favorites[] = $row;
}
$smarty->assign('Favorites', $favorites);
$db->query("SELECT tan_playlists.id, tan_playlists.foto, tan_playlists.name, tan_users.username, tan_users.fullname FROM tan_playlists LEFT JOIN tan_users ON tan_playlists.user_id = tan_users.user_id WHERE tan_playlists.user_id = '" . $user['user_id'] . "' ORDER by id DESC LIMIT 0,10");
while ($row = $db->get_row($sql_result)){
$playlists[] = $row;
}
$smarty->assign('Playlists', $playlists);
}
$smarty->assign("Title", $username);
if($member_id['username'] == $username) $smarty->assign("MEMBER", $member_id);
?>
Any ideas why i'd be getting this error?
Safe Mode is off on my hosting, and it's a free host so I can't change php.ini
I'm kinda a PHP noob, any help and link to solve my problem would be appreciated.
It seems like you haven't created the $db object.
First you have to initialize your DB-connection like
$db = new PDO('host'; 'table', 'user', 'pass');

how to access fetch value from database using mysql and this value use in same form any were

use query for access the $current_rank this value want to access in different query but this value can not access any where in different query so how to access $current_rank......
$query = "select * from menu_master where menu_id =
$row_id and hotel_id='" . $_REQUEST['hotel_id'] . "'";
$result = mysql_query($query)."<br/>";
while($row=mysql_fetch_array($result))
{
$rank = $row['set_rank'];
}
$current_rank = $rank;
//echo $current_id = $row_id."<br/>";
//echo $new_rank =$_REQUEST['set_rank']."<br/>";
$sql = "select * from menu_master where set_rank = '$new_rank ' and hotel_id='".$_REQUEST['hotel_id']."'" ;
// echo $sql."<br/>";
$rs = mysql_query($sql)."<br/>";
while($row = mysql_fetch_array($rs))
{
$menu_id = $row['menu_id'];
$sql="update menu_master
set set_rank=$current_rank where menu_id= $menu_id and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
mysql_query($sql)."<br/>";
}
$sql="update menu_master set menu_name = '" . mysql_real_escape_string($_REQUEST['menu_name']) . "',
menu_name_ar = '" . mysql_real_escape_string($_REQUEST['menu_name_ar']) . "',
is_active = '" . $is_active . "',
set_rank = $new_rank where menu_id = '$current_id' and hotel_id='".$_REQUEST['hotel_id']."'";
//echo $sql."<br/>";
//exit;
mysql_query($sql);
Your current_rank seems to be an array. If you have single value in current_rank, then do not use while loop for it.
Just use $row=mysql_fetch_array($result);
$current_rank = $row['set_rank'];
Also you have commented out this line.
//echo $new_rank =$_REQUEST['set_rank']."";
So you have no value for $new_rank

Categories