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);
Related
I need some PHP coding assistance in building an echo to display the results of a database query...(list friends of the logged-in user?) I have the majority of it built, but I don't know how to echo the results data and link it to his/her profile? Any help would be greatly appreciated...maybe a code snippet of how to write it out?
Here is my code:
$query = mysqli_query($db_conx, $sql);
$numrows = mysqli_num_rows($query);
if ($numrows > 0){
while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){
array_push($my_friends, $row["user2"]);
array_push($my_friends, $row["user1"]);
}
//remove your id from array
$my_friends = array_diff($my_friends, array($u));
//reset the key values
$my_friends = array_values($my_friends);
mysqli_free_result($query);
} else {
echo "You have no friends.";
}
// Build My friends From Results
****this is where I need help with*****
if (array_key_exists('0', $my_friends)){
$sql = "SELECT user1, user2
FROM friends
WHERE (user1='$u' OR user2='$u')
AND accepted='1'";
$result = mysqli_query($db_conx, $sql);
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
$my_friends = "<a href='user.php?u=".$result."'>". $row['avatar']. ' '. $row['firstname']. ' ' .$row['lastname'] ."</a>"; }
}
?>
Here is my html code:
<?php
echo "<li>$my_friends</li>";
?>
You are using the $result variable in the html link. Replace it with $row['id'] or the row attribute you use as GET parameter to see a user's detailed page
I want to make a login system, where the user can see all the messages he has received. In my database the insurance_company is the username and that is what I want it to show by. Here is my code. How do I fix it?
- Thanks Prem
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {
echo $row['Claim'] . "<br />";
}
?>
PHP code can be written anywhere in HTML code. You need to just place your PHP code between tags. As you can see in the example bellow I have used php code between p tags.
<?php
$strSQL = "SELECT * FROM claims WHERE insurance_company = $_SESSION['user']";
$rs = mysql_query($strSQL);
while($row = mysql_fetch_array($rs)) {?>
<p> <?php echo $row['Claim'] . "<br />"; ?> <p>
<?php }
?>
If you want it to show insurance_company, you must echo this value in the response array:
echo $row['insurance_company']
I have problems to get the right GET-method for my results.
I have a script which shows me some results of my database. It works with Ajax:
$suchbegriff = $_POST["suchbegriff"];
if ($_POST["suchbegriff"]){
$sql = "SELECT * FROM user WHERE vorname OR nachname LIKE '%$suchbegriff%'";
$result = $conn->query($sql);
echo "<br>Sie suchten nach: ".$suchbegriff."<br>";
while($row = $result->fetch_assoc())
{ echo "<form action=\"profil.php\" method=\"get\">".$row['nachname'];
echo $row['vorname']."</a> <input type=\"submit\">";
echo "<br/>";}
}
Now I want to get the ID of the search-results to bring it with GET/POST to an other page called profil.php (unique user-profile):
<div class="col-xl-12">
<div class="col-xl-2">
<p>Vorname</p>
</div>
<div class="col-xl-5">
<p><?php
$sql = "SELECT vorname FROM user WHERE ID = $_GET[nachname]";
$result = $conn->query($sql);
$row = $result->fetch_assoc();
echo $row['vorname'];
?></p>
</div>
</div>
Is there a way to couple nachname and vorname (name, surname) in the SQL-Statement?
And how can I get the unique profile-URL from the search-results?
Thank you.
Change your first script to print a list of anchors, and put the ID into the href URL.
while($row = $result->fetch_assoc())
{
echo "".$row['nachname']. " ".$row['vorname']."";
echo "<br/>";
}
I have a question which is very similar to this one: Display Album with Photos (PHP), however I need a bit of help in applying the code to my situation.
I have two tables, which are related by the column AlbumID:
Album: AlbumID, name, date(optional)
Image: ImageID, name, imageURL, AlbumID
My PHP code so far:
function select_galleryimage($sql,$a_id, $img_id) {
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0);
while ($row = $result->fetch_assoc()) {
echo "<div id='".$row['AlbumID']."'>\n
<a class='$a_id' href=".$row["imageURL"]." target='_blank'>
<img src=".$row["imageURL"]." alt='".$row['name']."'>
</a>\n
</div>\n";
}
}
where $sql = SELECT * FROM album JOIN image ON album.AlbumID=image.AlbumID'
$a_id = 'lightbox' and $img_id = ''
The issue that I am having is how to create a loop which creates the divs into which the loop with the images is inserted, so as the end result is something like this:
<div id="album1" style="display:none">
<h1>Gallery</h1>
<a class="lightboxX35" href="media/Photos/_MG_7732.jpg"><img src="media/Photos/_MG_7732.jpg"></a>
<a class="lightboxX35" href="media/Photos/_MG_7508.jpg"><img src="media/Photos/_MG_7508.jpg"></a>
</div>
Gallery
Thank you all in advance for any help provided.
Somehing like this?
if ($result->num_rows > 0) {
$current_album = null;
while ($row = $result->fetch_assoc()){
if ($current_album <> $row['AlbumID']) {
if (!empty($current_album))
echo "</div>\n";
echo "<div id='".$row['AlbumID']."'>\n";
echo "<h1>Gallery</h1>\n";
$current_album := $row['AlbumID'];
}
echo "<a class='$a_id' href='".$row["imageURL"]."' target='_blank'><img src=".$row["imageURL"]." alt='".$row['name']."'></a>\n";
}
echo "</div>\n";
}
I haven't tried it on a server so may not be the correct synthax...
With more than a single album you have to sort the SQL by album using ORDER BY then echo the DIV fo every new album. To achieve this assign a variable $current_album = null; outside the while loop then check for changes inside.
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