I'm having a strange error, and I know Im posting a lot of code, but, Im really not understanding what Im doing wrong, so I decided to put all the code here related to my problem.
I also commented my code to be easily to understand context.
The problem is:
Im bringing my news information from database, and Im having a problem with images, only the image of the first post, which is the recent news, is appearing, the others dont appear.
However if I delete the code that I quoted like FROM HERE* **TO HERE*, images appear correctly.
Someone there realize why this is happening in this code excerpt that causes the images of my news do not appear?
<div id="content">
<h1>News</h1>
<?php
//first I read my news
$readNews = $pdo->prepare("SELECT * FROM news WHERE status = 1 ORDER BY date DESC LIMIT 0,4");
$readNews->execute();
//while exist news I want to read category of that news, and show news info in my article
while ($readNewsResult = $readNews->fetch(PDO::FETCH_ASSOC))
{
$readCat= $pdo->prepare("SELECT * FROM categories , news WHERE id_cat = ?");
$readCat->bindValue(1,$readNewsResult['category']);
$readCat->execute();
$resultReadCat = $readCat->fetch(PDO::FETCH_ASSOC);
//I show news info in my article
echo '<article class="loop-news">';
echo '<img src="'.$path.$readNewsResult['thumb'].'" />';
echo '<h2>'.$readNewsResult['title'].'<br /></h2>';
echo '<span>'.$resultNewsResult['date'].'</span>';
echo '<p>'.$readNewsResult['content'].'<a id="'.$readNewsResult['id_not'].'" class="fancybox" href="#show'.$readNewsResult['id_not'].'">See more</a></p>';
//I repeat my news info to show inside fancybox
echo '<div class="show-container">';
echo '<div id="show'.$readNewsResult['id_news'].'">';
echo '<h2>'.$readNewsResult['title'].'</h2>';
echo '<span>'.$resultNewsResult['date'].'</span>';
echo '<img src="'.$path.$readNewsResult['thumb'].'" />';
echo '<p>'.$readNewsResult['content'].'</p>';
//******************FROM HERE***************************/
//I read my table links_news to see if news that is reading have links associated
$readLinks = $pdo->prepare("SELECT * FROM links_news WHERE news_id = ?");
$readLinks->bindValue(1,$readNewsResult['id_news']);
$readLinks->execute();
//if current reading news have links it will show on my links div
echo '<div class="links">';
echo '<h3>Relative Links:</h3>';
echo '<ul class="links1">';
while ($readLinksResult = $readLinks->fetch(PDO::FETCH_ASSOC))
{
echo '<li> '.$readLinksResult['link'].'</li>';
}
echo '</ul>';
echo '</div>';<!-- close class links-->
//****************TO HERE*******************/
echo '</div>';<!-- close class show -->
echo '</div>';<!-- close class show-container-->
echo '</article>';
}
?>
</div>
Related
I have created a members.php page that connects to a database table. The table has the following fields: id, username, profile image.
The following PHP code displays the profile image for each user in rows of 6 and allows each image to be clickable.
<?php
// The code below will display the current users who have created accounts with: **datemeafterdark.com**
$result=mysql_query("SELECT * FROM profile_aboutyou");
$row1 = mysql_fetch_assoc($result);
$id = $row1["id"];
$_SESSION['id'] = $id;
$profileimagepath = $row1["profileimagepath"];
$_SESSION['profileimagepath'] = $profileimagepath;
$count = 0;
while($dispImg=mysql_fetch_array($result))
{
if($count==6) //6 images per row
{
print "</tr>";
$count = 0;
}
if($count==0)
print "<tr>";
print "<td>";
?>
<center>
<img src="<?php echo $dispImg['profileimagepath'];?>" width="85px;" height="85px;">
</center>
<?php
$count++;
print "</td>";
}
if($count>0)
print "</tr>";
?>
This is all great, however, when I click on the image that loads it re-directs me to: viewmemberprofile.php which is what it is supposed to do. But it always displays the same image with the same id value (i.e.) 150 no matter which image I click. What I would like to have happened is. If I click on an image with id 155 etc... it will display content for that image data field not consistently the same image data regardless of which image I click.
Your help and guidance would be greatly appreciated. Thank you in advance.
One thing that I forgot to mention is that I do use sessions so... when I am re-directed to the viewmemberprofile.php page I use the following code to aide in getting the data that I need from the table.
<?php
$id = $_SESSION['id'];
echo($id);
?>
<?php
echo('<br>');
?>
<?php
$profileimagepath = $_SESSION['profileimagepath'];
?>
<img src="<?php echo($profileimagepath);?>" width="50px;" height="50px;">
I have yet to impliment the suggested solution.
You need to pass the ID of the row to viewmemberprofile.php, e.g.:
<a href="viewmemberprofile.php?id=<?= $dispImg['profileimagepath'] ?>">
And viewmemberprofile.php needs to select that row from the DB:
SELECT * FROM profile_aboutyou WHERE id = $_GET['id']
The above SQL statement is pseudo-code; you need to write actual code to accomplish what it is describing, preferably using parameterized queries.
I am working on a school project, a simple webshop.
I decided to ask it here.
What i want to do is display products from my database to my "product page" using PHP.
For some reason i only get the last product to show, also having trouble with the images.. I know the query is correct.
"SELECT * FROM products";
Here is my code.
This is my PHP, I have put this on top of the product.php page.
product.php
<?php
session_start();
include "includes/connectie.php";
include "includes/navbar.php";
$vraag = "SELECT * FROM products";
//var_dump($vraag); Var dump test
$resultaat = $conn->query($vraag);
if ($resultaat->num_rows > 0)
{ // Min. 1 row returned
while ($rij = $resultaat->fetch_assoc())
{
$id = $rij['id'];
$titel = $rij['product_name'];
$prijs = $rij['product_price'];
$omschrijving = $rij['description'];
$foto = $rij['image'];
}
}
else
{
// Do nothing
}
?>
This is my HTML code below, where i want to "display" i also have this in the same file as code above. So first php, then html
product.php
<body>
<div class="products">
<div class="productContainer">
<h1 id="banner">ALL ITEMS</h1>
<div class="productRow">
<?php
echo '<img src="img/product_'.$id.'.jpg">';
?>
</div>
<?php
echo '<h1>'.$titel.'</h1>';
echo '<p class="prijs">'.$prijs.'</p>';
echo '<p class="omschrijving">'.$omschrijving.'</p>';
?>
<div class="productRow">
<?php
echo '<img src="product_'.$id.'.jpg">';
echo '<h1>'.$titel.'</h1>';
echo '<p class="prijs">'.$prijs.'</p>';
echo '<p class="omschrijving">'.$omschrijving.'</p>';
?>
So i only put the HTML/PHP where i would want the first two products as example.
In total i have 6 products. I also included a picture of the database
database
What am i doing wrong?
This is how it looks right now:
example of product display
in your php file you have added all the values from database to an array. but you have not used any loop when you are trying to display array values in html. loop is a must to display all the values of array. Add a loop in your HTML file.
I have a huge problem and been having a hard time figuring it out.I want to pass down the image id i click on onto the 2nd page.The 2nd page then uses the id and echos out an image,image_text based on what i gave in the database.
1st page
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='img-single'>
<a href='images/fpage.php?image_id={$row['image_id']}'><img class='i'
src='images/".$row['image']."' ></a>
<figcaption class='figcaption'>".$row['image_text']."</figcaption>
</div>";
}
?>
2nd page
<?php
$image_id = $_GET['image_id'];
?>
So after passing the image id in a a href='somepage.php&image_id=$image' tag. On the 2nd page use: if (isset($_GET['image_id'])) { $pic_id = $_GET['image_id'] Then make a select from database using pic_idmysqli_query($dbc, "SELECT picture, name from table_name WHERE picture_id = '$pic_id' LIMIT 1;"); echo the picture in image tags and your done.
<img src="<?php echo $picture; ?>" alt="<?php echo $name; ?>">} // Close if(issset()) I hope this helps.
I am building a forum in order to practice and i am having trouble getting a forum ID from the URL and later on the next page i am getting a "Call to undefined function" error. What is is that im doing wrong?
Here is how i pass the forum id from a href, from the main forums page.
echo "<a href=forum.php?forum=" . urlencode($array_forums["forum_id"]) . ">";
Here is how i get the id inside my topics page, where i should display all topics, related to this forum id.
<?php
if (isset($_GET['forum'])) {
$forum_id = $S_GET['forum'];
}
?>
<div id="topics_container">
<?php
//1. Perform database query for topics
$sql_topic = "SELECT * FROM topics WHERE topic_forum_id = {$forum_id}";
$res_topic = mysqli($conn, $sql_topic);
//2. Display returned data from topics
while ($array_topic = mysqli_fetch_assoc($res_topic)) {
echo "<div id='topic_{$array_topic["topic_id"]}'";
echo "</div>";
}
?>
</div>
I found my errors, problem solved.
$forum_id = $_GET['forum'];
instead of
$forum_id = $S_GET['forum'];
and
mysqli_query($conn, $link)
instead of
mysqli($conn, $link)
I have a simple page which contains some products in the database. What I want is to create some sort of an updater, if the user add a product then that specific div in the page will display the latest product added automatically.
I tried to view this link but it doesnt help at all we have different problem.
Anyway heres the function i have in php.
function recentlyAddeditems() {
echo '<h3>Recently Added Products</h3> <hr/>';
echo '<div id="main_section_widget">';
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC LIMIT 20") or die(mysql_error());
if ($sql) {
while ($row = mysql_fetch_array($sql)) {
$name = $row['name'];
$id = $row['id'];
$category = $row['category'];
echo "$name | $category<br/>
<hr/>
";
}
echo '</div>';
echo '
<p><br/>More items...</p>
';
}
}
and the in html page
<div id="main_section_widget_cover" class="dates">
<?php recentlyAddedItems(); ?>
</div>
Is there a simpler way how to do this? I tried to read about ajax in jquery but still having confused without a concrete example.
Please help wizards of stack!!!
IF you want an example of a ajax in jquery here is a really simple example
<div id="main_section_widget_cover" class="dates"><div>
<script>
$('#main_section_widget_cover').load('recentitems.php');
</script>
in recentitems.php
<?php recentlyAddedItems(); ?>
Thats it, thats all you need to do to do a basic ajax call