Create a News Feed that displays posts from people they are following - php

I have a code that should check the Database following to find all the people that the current logged in user has followed then it will search the second database posts to find any posts by the people they are following but it currently only shows one post. I know its not very efficient compared to JOIN but I don't fully understand how that works.
Here is a Screenshot of the Database Following it has the user who sent the following request and the user they are following what is it should do is use the session id for the logged in user and find all matching results for the user in the following column and it should take all the results from the column following and search the database post in the column code and echo all the results as the newsfeed.
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD","DB");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$myid=$_SESSION['user']['id'];
$result = mysqli_query($con,"SELECT * FROM following WHERE follower='$myid'");
while($row = mysqli_fetch_array($result)) {
$following=$row['following'];
?>
<?php
$con=mysqli_connect("HOST","USERNAME","PASSWORD","DB");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM posts");
while($row = mysqli_fetch_array($result)) {
if ($following==$row['code']){
?>
<div class="content">
<section class="intro">
<h2><?php echo $row['name']; ?> <?php echo $row['lastname']; ?></h2>
<div class="text">
<?php echo $row['post']; ?>
</div>
<?php
}
else{
}
}
mysqli_close($con);
?>
<?php
}
mysqli_close($con);
?>

Because my previouse answer didnt help What do you want to output??
If your database has 3 followers does it have to output this in html?
But this Code will Output All users in your following list Like this..
$result = mysqli_query($con,"SELECT * FROM posts");
echo "<div class='content'> <section class='intro'>";
echo "<h2><a href='/profile/?id=".$row['code']."'>";
while($row = mysqli_fetch_array($result)) {
if ($following==$row['code']){
echo $row['name']." ".$row['lastname']."</a></h2>";
echo $row['post'];
}
echo "<div class='text'>";
echo "</div>";
}
This Code is Outputting this but the Bold text in this example is a link that goes to
/profile/?id=CODEHERE
Steve Jobs <-- is a link (goes to /profile/?id=CODEHERE
The Post text
Johny John
The Post text
Miracle Tru
The Post text

Related

display content containing html tags from database using php

Table name : Post, column name : content
say, below data along with the html tag and style is stored in content column
<h1 style="color:red">test</h1>
I'm using below php code to display it
<?php
$sql = "SELECT content FROM post where id = 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<?php echo htmlspecialchars($row['content'], ENT_QUOTES); ?>
<?php
}
}
else {
echo "0 rasdesults";
}
$conn->close();
?>
expectation : test in red color but
output : only "test"
Please replace you echo syntaxt
<?php echo htmlspecialchars($row['content'], ENT_QUOTES); ?>
with below line
<?php echo htmlspecialchars_decode($row['content']);?>
Hope this will help you!
Thanks & regards.
Shishil Patel

Insert into mysql using div codes and php

the below to functions contain the code to insert into the sql database but sadly the db is still unable to load it to the database.
if (isset($_POST['register'])){
if(registerNewUser($_POST['inv_amount_expected'],$_POST['uname'],$_POST['passwo rd'],$_POST['email'])){
echo "You can now log-in to your account.
<a href='./index.php'>Click here to login.</a>
";
}else {
echo "Registration failed! Please try again.";
show_registration_form();
}
} else {
// has not pressed the register button
show_registration_form();
}
function registerNewUser($inv_amount_expected,$uname,$password,$email)
{
$sql = sprintf("insert into borrow (inv_amount_expected,uname,password,email) value ('&inv_amount_expected','&uname','&password','&email')",
mysql_real_escape_string($username), mysql_real_escape_string(sha1($password . $seed))
, mysql_real_escape_string($email), mysql_real_escape_string($code));
if (mysql_query($sql))
{
$id = mysql_insert_id();
return true;
}
else
{
return false;
}
return false;
}
could you help me out in where i am going wrong since im unable to understand.
i'm still an amature in php so please help me out.
I am putting my ideas together and at the moment i have the following:
-a mysql database with 2 tables.
-the first table "downloads" contains 3 rows, each with a unique id that and each represent the type of file being downloaded. e.g. application or theme.
-the second table contains the information about each download, these fields are the models supported by the download, the title, a picture, a brief description and a download link. each download has a unique id.
Now what i am trying to do is insert the data into a set of divs, these divs are as follows:
<div class="dlcontainer">
<div class="dlitem">
<div class="dltitle"><php $row['title'] ?></div>";
<div class="dlimage"><php $row['image'] ?></div>";
<div class="dldescription"><php $row['description'] ?></div>";
<div class="dllink"><php $row['downlink'] ?></div>";
</div>
</div>
As you can see i have been trying to populate the divs with information called from the database and that was my first attempt.
I didn't feel like i was going about this with the right approach, so i ended up with this, which also does not seem to work:
<?php
$con = mysql_connect("test","test","test");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("test", $con);
$result = mysql_query("SELECT * FROM `content` WHERE pid = '2' AND models = 'all' OR models = $chosen");
while($row = mysql_fetch_array($result))
{
echo "<div class=\\"dlcontainer\\">";
echo "<div class=\\"dlitem\\">";
echo "<div class=\\"dltitle\\">$row['title']</div>";
echo "<div class=\\"dlimage\\">$row['image']</div>";
echo "<div class=\\"dldescription\\">$row['description']</div>";
echo "<div class=\\"dllink\\">$row['downlink']</div>";
echo "</div>";
echo "</div>";
}
mysql_close($con);
?>
Once i get this initial download working, i can then set up a loop that will display all the contents that match belong to a specific "pid" and either match a "models" value of "all" or one that has been selected by the user.

Send ID instead Name in PHP and MYSQL

Problem:
i want to send ID to database table instead of Name. but i want to display Name in a field instead of ID.
it work's well while i send it through combo box. but i don,t know how it work with Search field. the php code is given below:
<font> <b>Name: </b></font>
<?php
include("database/db.php");
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sql = "SELECT * FROM table";
$result = $link->query($sql); ?>
<select name="ID">
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$ad_id = $row["ID"];
$name= $row['Name'];
?>
<option value="<?php echo $ad_id; ?>"><?php echo $name; ?></option>
<?php
}
} else {
echo "0 results";
}
// close connection
mysqli_close($link);
?>
</select>
As per your comment you can use the auto complete text box in php from mysql by using ajax call it will fulfill your requirement for search by name.
There are some link available in web as you can search and implement but you have to do some changes for the ID contact with NAME
Autosuggest ref 1
Autosuggest link ref 2
JQUERY UI

My fetch appears not to get any results

I want to use this code to fetch content out of the database, database connection is made with global $connect and is working correctly. But I do not understand why there is no content available on the web page. If someone can help me with this, that would be very appreciated.
<div class="content">
<article class="fullcontent">
<header>
<h2>Advertenties</h2>
</header>
<content>
<?php
global $connect;
$sql2 = "SELECT * FROM advertentie";
$result2 = $connect->query($sql);
if ($result->num_rows > 0){
while ($row = $result -> fetch_assoc()){
echo "<div class='fixed-groups'>";
echo "<a class='fixed-group-item' href='website'>";
echo "<div class='image'>";
echo "<img src='cms/pages/afbeeldingen/".$row['filename']."' width='173px' height='138px' />";
echo "</div></a>";
echo "<strong>".$row['advertentie_titel']."</strong>";
echo "<span>".$row['verkoopprijs']."</span>";
echo "</div>";
}
}
?>
</content>
If someone got some tips for me on how to improve this, many thanks! I don't know how to assign a href to a inserted form, so when I insert a form that form also has to get his own web page. how can I manage that?
You assigned the sql query to $sql2 and ran $sql in $connect->query($sql)
Correct would be:
$sql2 = "SELECT * FROM advertentie";
$result2 = $connect->query($sql2);

random image not working correctly

i have create an array to diusplay a random file name from my mysql database. unfortunatly it doesnt show correctly.
i need the explode to work based on file id to show the correct banner picturse for that tv series.
<?php include '../connect/dbseries.php' ?>
<?php include 'Sbarray.php' ?>
<?php
$names = explode ("|", $row['4']);
?>
<center><?php
while($row=mysql_fetch_array($result2)){
echo '<img src="../images/series/'. $names[array_rand($names,1)].'" width="800" height="150" style="padding:2px;">';
}
?>
</center>
my array page is
<?php $result2 = mysql_query("SELECT
ID,
pretty_name,
sortname,
Genre,
Bannerfilenames,
CurrentBannerFilename,
Posterfilenames,
PosterBannerFileName,
summary,
Fanart,
IMDB_ID
FROM online_series
order by sortname ASC;");
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
?>
<?php $row = mysql_fetch_row($result2);
//setup array
$banner = $row['4'];
?>
that is all the code i have on the page. any help would be appreciated as it only shows images from row 1 instead of for each row/ tv series
i asume it has something to do with the explode command but cant figure out how to correct it.
thanks in advance
you can check with print_r($names) whether it works. If I understand your problem correctly, you want a random pic of each row. Now you do the variable names before you iterating through the results - so it uses always the first:
<?php $result2 = mysql_query("SELECT
ID,
pretty_name,
sortname,
Genre,
Bannerfilenames,
CurrentBannerFilename,
Posterfilenames,
PosterBannerFileName,
summary,
Fanart,
IMDB_ID
FROM online_series
order by sortname ASC;");
if (!$result2) {
echo 'Could not run query: ' . mysql_error();
exit;
}
?>
<center><?php
while($row=mysql_fetch_assoc($result2)){
$names = explode ("|", $row['Bannerfilenames']);
//for check whether explode works
print_r($names);
echo '<img src="../images/series/'. $names[array_rand($names,1)].'" width="800" height="150" style="padding:2px;">';
}
?>

Categories