How to display user avatar next to post? (php,mysql) - php

So I have two tables in my database one is called Users and the other is News
I made it that users can add News Posts to the site, but I couldn't display user's image next to his post
this is my code right now
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src=\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
I want where is says $row2['author_avatar'] to echo the image from the users table

You missed out a speech mark (") just before $row2['author_avatar'].
<?php
$News = "";
$user_id = "";
$sqlCommand = "SELECT * FROM News ORDER BY id DESC LIMIT 10";
$sqlCommand3 = "SELECT * FROM Users";
$query = mysql_query($sqlCommand) or die(mysql_error());
$query3 = mysql_query($sqlCommand3) or die(mysql_error());
$count = mysql_num_rows($query);
if($count > 1){
$News .= "";
while(($row = mysql_fetch_array($query)) && ($row2 = mysql_fetch_array($query3)) ){
$News .= "<div class=\"news-post\"> <img src="\".$row2['author_avatar']."\"><p>".$row['author']."</p> <h2>".$row['title']."</h2></div> ";
} // close while
} else {
$News = "No News!";
}
?>
You should look up INNER JOIN rather than having two SQL queries as mentioned by andrewsi. Here's a good tutorial on how to use it https://www.youtube.com/watch?v=6BfofgkrI3Y.

Related

Show different data in webpage when clicked depending on the ID

I am trying to show user data when clicking link depending on a story ID. However, the page is showing the same data from one row in the database every time (web link is showing a different ID 'php?story_id=10' etc. but it's still showing the same data.
I have this function:
function displayStories(){
include('conn/conn.php');
$queryread = "SELECT Users.ID, Users.FirstName, Stories.Age, Stories.Story,
Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID";
$result = mysqli_query($conn, $queryread) or die(mysqli_error($conn));
if(mysqli_num_rows($result) > 0 ){
while($row = mysqli_fetch_assoc($result)){
$name = $row["FirstName"];
$idData = $row["ID"];
$age = $row["Age"];
$story = $row["Story"];
$limit = mb_strimwidth($story, 0, 300, "...");
echo
"<div class='stories'>
<a href='storyDis.php?story_id=$idData'>
<h5><b>$name</b></h5></a>
<p><b>$age years old</b></p>
<p>$limit</p>
</div>
";
}
}
mysqli_close($conn);
}
And this to query to display the data(variables called in HTML):
include('conn/conn.php');
$str = htmlentities($_GET["story_id"]);
$query = "SELECT * "
. "FROM Stories "
. "WHERE ID='$str'";
$result = mysqli_query($conn, $query) or die(mysqli_error($conn));
$queryread = "SELECT Users.FirstName, Stories.Age, Stories.Story,
Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID";
$result1 = mysqli_query($conn, $queryread) or die(mysqli_error($conn));
if(mysqli_num_rows($result1) > 0 ){
while($row = mysqli_fetch_assoc($result1)){
$name = $row["FirstName"];
$age = $row["Age"];
$story = $row["Story"];
$image = $row["Image"];
}
}
mysqli_close($conn);
?>
EDIT
Had two queries when I only needed one.
$str = htmlentities($_GET["story_id"]);
$queryread = "SELECT Stories.ID, Users.FirstName, Stories.Age, Stories.Story, Stories.Image FROM `Stories` INNER JOIN `Users` ON Users.ID = Stories.User_ID
WHERE Stories.ID='$str'";
$result1 = mysqli_query($conn, $queryread) or die(mysqli_error($conn));
if(mysqli_num_rows($result1) > 0 ){
while($row = mysqli_fetch_assoc($result1)){
$name = $row["FirstName"];
$age = $row["Age"];
$story = $row["Story"];
$image = $row["Image"];
}
}
mysqli_close($conn);
?>
you are reading the data from (result1) not from (result).
Change this one while($row = mysqli_fetch_assoc($result1)){
to
while($row = mysqli_fetch_assoc($result)){
and then it will works fine with you .

Display a new div after every number of records fetch from database in MySQL

I want to create a div for ads on a project I'm working on. I want the div to show after 8 records is fetch from the database.
My select statement:
$query = mysql_query("SELECT * FROM posts WHERE `username` = '$followe' OR `user_id` = ' $get_id' ORDER BY date_added DESC LIMIT 15 ");
$newsCount = mysql_num_rows($query); // Count the output amount
if ($newsCount > 0) {
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$user_post_id = $row["user_id"];
$username = $row["username"];
$text = $row["texts"];
$profile_pix = $row["profile_pix"];
}
}
You need to add a counter for your while loop and echo the HTML string when the counter is divisible by 8:
<?php
$query = mysql_query("SELECT * FROM posts WHERE `username` = '$followe' OR `user_id` = ' $get_id' ORDER BY date_added DESC LIMIT 15 ");
$newsCount = mysql_num_rows($query); // Count the output amount
if ($newsCount > 0) {
$rowcount=1;
$divhtml = "<div></div>";
while ($row = mysql_fetch_assoc($query)){
if ($rowcount % 8 == 0) {
echo $divhtml;
}
$rowcount++;
$id = $row["id"];
$user_post_id = $row["user_id"];
$username = $row["username"];
$text = $row["texts"];
$profile_pix = $row["profile_pix"];
}
}
?>
You should change the content of variable $divhtml, with your own ad HTML.
Use an accumulator pattern such as this.
$i = 0
while($row = mysql_fetch_assoc($query)){
$i++;
if(i == 8) {
// DO WHATEVER
}
$id = $row["id"];
$user_post_id = $row["user_id"];
$username = $row["username"];
$text = $row["texts"];
$profile_pix = $row["profile_pix"];
}

mysql SELECT WHERE query inside elseif loop not doing anything and no error

I run my script ( see below ) but the place where industry & keywordrw vars are set it just doesn't do anything
The other queries where just the industry is set works fine
the other 2 that involve doing multiple LIKE queries on the keyword variable don't do anything
<?php
if (!isset($industry)) {
$industry = '';
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw;
} elseif (!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $industry;
} elseif (isset($keyword) && ($industry)) {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
echo $keywordrw . '-' . $industry;
} else {
mysql_select_db($dbn, $dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100", $dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}
?>
Changed a few things and updated working code is below
if(!isset($industry))
{
$industry = '';
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE title LIKE '%keywordrw%'",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(!isset($keyword)) {
$industry = $_GET['industry'];
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE category LIKE '$industry' LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} elseif(isset($keyword) &&($industry)){
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings WHERE (title LIKE '%$keywordrw%' OR description LIKE '%$keywordrw%') AND (category = '$industry') LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
} else {
mysql_select_db($dbn,$dbc);
$limit = 100;
$query = mysql_query("SELECT * FROM listings LIMIT 0,100",$dbc);
$result = mysql_fetch_assoc($query);
echo mysql_error();
//echo $keywordrw.'-'.$industry;
}

join query not working properly

I want to select two table values so I am using join query see below, from this code I stored one session variable like $_SESSION['emp_id'], I want select query like who are in te.emp_id='".$_SESSION['emp_id']."', From this code $count will return 0 but in my database I have one row.
$q = mysql_query("SELECT *
FROM task_employee te
JOIN task t
ON te.emp_id = t.t_assign_to
WHERE t.t_status = '0'
AND t.t_delete_on != '1'
AND te.emp_id = '" . $_SESSION['emp_id'] . "'");
$data = array();
while($r = mysql_fetch_assoc($q))
{
$data[] = $r;
}
$count = sizeof($data);
if($count > 0)
{
$return = array('status'=>'success', 'count'=>sizeof($data), 'data'=>$data);
echo json_encode($return);
}
else
{
$return=array('status'=>'not-found', 'count'=>sizeof($data), 'data'=>$data);
echo json_encode($return);
}
I am writing like this means I can get but using join query that time I can't get values
<?php
$sql = mysql_query("SELECT *
FROM task
WHERE t_delete_on !='1'
AND t_assign_to = '$emp_id'");
for ($i = 1; $row = mysql_fetch_assoc($sql); $i++)
{
$assign_to = $row['t_assign_to'];
$mysql = mysql_query("SELECT *
FROM task_employee
WHERE emp_id = '$assign_to'");
while ($r = mysql_fetch_assoc($mysql))
{
$emp_name = $r['emp_firstname']; // here i got value
}
}
?>

Loop for Arrays in php

<?php
session_start();
$link = mysqli_connect("localhost", "xxx", "xxxxxx", "xxx");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$id = $_GET['id'];
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result))
{
$idS = $row['SWho'];
echo $idS;
if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result))
{
echo "<img src='Member_ProfilePics/";
echo $row['PP'] . '.' . $row['Ext'];
echo "' width=42 height=40 style='float: left'>";
}
}
}
//<img src='Member_ProfilePics/john.jpg' width=42 height=40 style='float: left'>
}
?>
here in my code, i want to output the list of STo. and connect to other database and output the following rows in each STo.
Sample to be output:
(picture of id=1) 1
(picture of id=2) 2
(picture of id=3) 3
(picture of id=4) 4
(picture of id=5) 5
(picture of id=6) 6
How?
my code is not looping.
Sample output of my code:
(picture of id=1) 1
You are using the same variables for both the loops:
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
....
if ($result = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
...
}
}
}
}
Try to change the seconda value to these
if ($result = mysqli_query($link, "SELECT * FROM Subscribe WHERE STo = '".$id."' ORDER BY ID LIMIT 6")) {
while($row = mysqli_fetch_array($result)) {
....
if ($result2 = mysqli_query($link, "SELECT * FROM accounts WHERE ID = '".$idS."' ORDER BY ID LIMIT 6")) {
while($row2 = mysqli_fetch_array($result2)) {
...
}
}
}
}

Categories