I have user information coming from database on a profile page using a while loop. I want to be able to write stuff like this, $username's profile. I can't seem to figure it out. Here is my code.
<?php
//open database connection
include 'page-start.php';
include 'core/init.php';
?>
<?php
$myQuery = ("SELECT user_id, username, profile, city FROM `users` WHERE user_id = '" . mysql_real_escape_string($_GET['ID']) . "' ") or die(mysql_error());
//run query
$result = $con->query($myQuery);
if (!$result) die('Query error: ' . mysqli_error($result));
if($result === FALSE) {
die(mysql_error()); // TODO: better error handling
} ?>
<?php require_once $_SERVER['DOCUMENT_ROOT'] . '/studentsupport/defines.php'; ?>
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
echo '<div class="sixteen columns" id="user-profile">';
echo'<h2 class="username"> ' . $row['username'] . ' </h2> ';//bob's Profile
echo'<p>' . $row['city'] . '</p>'; //city: london
echo '<div class="eight columns" id="user-profile-img">';
echo'<img src="'. $row['profile'] . '"/>';
echo '</div>';
echo '</div>';
}
?>
edit: sorry I didn't explain it very well.
I want to be able to have some information come from the database and some information just as standard html for example:
<p><?php echo $username; ?>'s profile </p>
<p>city: <?php echo $city; ?> </p>
During each loop in the while, $row['username'] will have a username.
If you want to be able to get all the usernames later, add the following:
$users[] = $row['username'];
Now, Everywhere in your script $users[0] will have the 1st username, $users[1] will have the second, etc.
If you are beginner than i wil say You should use easy method now it seems okay
like You make Whole page of profile like you want
like
then you can Take your data in Variables in while Loop like that
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{
$profile = $row['profile'];
$name = $row['name'];//you can take as many variables as you want
$city = $row['city'];
}
then you can use these variables in html simply
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $name; ?> </h2>
</div>
start like that next step like above will get easy to u :)
This may do it.
<?php include_once("head.php");
while ($row = $result->fetch_array(MYSQLI_ASSOC))
{ ?>
<div class="sixteen columns" id="user-profile">
<h2 class="username"><?php echo $row['username']; ?></h2>
<p><?php echo $row['city']; ?></p>
<div class="eight columns" id="user-profile-img">
<img src="<?php echo $row['profile']; ?>"/>
</div>
</div>
<? } ?>
Related
When I run the following file I get the database data i.e it prints it out on the website so I know my connections are good.
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo $row["name"], $row["image"];
}
?>
</div>
</html>
However when I try and format the results like below
<html>
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo <div id = "bookbar">
<img src= "$row['image']" alt = "image">
<p> $row['name'] </p>
</div>
}
?>
</div>
</html>
it doesn't work. Can anyone help me fix the code?
Maybe try this your code didn't close/open the php tags properly also don't echo like that
<?php include 'config.php'?>
<?php include 'header.php'?>
<?php
$sql = "SELECT name, image FROM images";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<div id = "bookbar">
<img src= "<?php echo $row['image'] ?>" alt = "image">
<p><?php echo $row['name']; ?></p>
</div>
<?php
}
}
$conn->close();
?>
If you want to echo something out
Its better to close on open the php tags like so
PHP goes here
?>
HTML goes here
<?php
PHP goes here
And if you want to echo something inside the HTML just do this
<span> <?php echo "something" ?> </span>
much easier and makes the code easier to read.
change your echo statement to -
echo '<div id = "bookbar"><img src= "' . $row['image'] . '" alt = "image"><p>'. $row['name'] .'</p>'
Your issue is a syntax problem - you can't use echo like that, it has to echo a string variable. You should be seeing an error message about it.
You could keep the echo statement and put all the HTML inside a string, and concatenate (or interpolate) the PHP data into it. But IMO the easiest thing here in terms of readability and maintenance is to step out of the PHP tags, print the HTML, embed some PHP tags in it for the variables, and then step back in again to continue with the code. It makes the HTML far easier to understand:
?>
<div id="bookbar">
<img src="<?php echo $row['image'] ?>" alt="image">
<p><?php echo $row['name'] ?></p>
</div>
<?php
When you are in php mode you should echo strings as php variables wrapped with single quotes:
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<div id = "bookbar">';
echo '<img src="' . $row['image'] . '" alt = "image">';
echo '<p>' . $row['name'] . '</p>';
echo '</div>';
}
code:
<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
<div class="box2">
<?php
//session_start();
$sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
include 'php/dbconnection.php';
$conn = OpenCon(); //$conn=$_SESSION['conn'];
$result = mysqli_query($conn,$sql);
if($result){
$rows = mysqli_fetch_array($result);
echo "<div class='wrapper tab-content'>";
echo "<section class='col1'>";
echo "<h4><span>" . $rows['date']. "</span> </h4>";
echo "<p class='pad_bot2'><strong>". $rows['title']. "
</strong></p>";
echo "<p class='pad_bot1'>".$rows['des'] ."</p>";
echo "</section>";
echo "</div>";
}
?>
</div>
</div>
</article>
<!-- /content-->
Here I'm trying to, print 'news' table from database, but I'm unable to print anything on webpage.
can anyone point out, what is wrong in this ?
Thank You in advance for any help!
You have to put mysqli_fetch_row($result) inside a loop.
Example with your case:
if($result){
while ($rows=mysqli_fetch_row($result)){
echo "<div class='wrapper tab-content'>";
echo "<section class='col1'>";
echo "<h4><span>" . $rows['date']. "</span> </h4>";
echo "<p class='pad_bot2'><strong>". $rows['title']. "
</strong></p>";
echo "<p class='pad_bot1'>".$rows['des'] ."</p>";
echo "</section>";
echo "</div>";
}
}
// Free result set
mysqli_free_result($result);
Hope this helps!
For commented $conn=$_SESSION['conn'];
You cannot store resource handles within a $_SESSION. See PHP.net:
Some types of data can not be serialized thus stored in sessions. It
includes resource variables or objects with circular references (i.e.
objects which passes a reference to itself to another object).
As your object contains the connection link it can't be serialized, Just reconnect with every request.
And you can make clean template like below
<!-- content -->
<article id="content" class="tabs">
<div class="wrapper">
<div class="box2">
<?php
//session_start();
$sql = "SELECT * FROM `db`.`news` ORDER BY `date` DESC LIMIT 20";
include 'php/dbconnection.php';
$conn = OpenCon();
$result = mysqli_query($conn,$sql);
if($result):
while ($row=mysqli_fetch_row($result)):
?>
<div class='wrapper tab-content'>
<section class='col1'>
<h4><span><?php echo $row['date'];?></span></h4>
<p class='pad_bot2'>
<strong><?php echo $row['title'];?></strong>
</p>
<p class='pad_bot1'><?php echo $row['des'];?></p>
</section>
</div>
<?php
endwhile;
else:
?>
<p>No active news</p>
<?php
endif;
?>
</div>
</div>
</article>
<!-- /content-->
// Numeric array
$row=mysqli_fetch_array($result,MYSQLI_NUM);
// Associative array
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
So for your code, you need to change:
$rows = mysqli_fetch_array($result);
to:
$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
Also, you should check for number of rows before fetching by using:
$rowcount=mysqli_num_rows($result);
Only if $rowcount is more than 0, you should mysqli_fetch_array.
Below is my code for display information of an image including its file, but this is not displaying, I don't know why.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
$name = $row['name'];
$content = $row['content'];
$size = $row['size'];
$type = $row['type'];
$date_upload = $row['date_upload'];
$file = $row['file'];
}
}
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $name; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $date_upload; ?><br>type: <?php echo $type; ?><br>size: <?php echo $size; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file'] ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $content; ?></div>
</div>
First of all use inspect to check whether the image is actually pointed to by the code. that is the actual loation might not be pointed at. what is your folder structure.
" style="width:130px; height:100%">
your code is ok. as long as php echo $row['file'] returns a file
The problem here is "Your all the variables are local to the while loop". so you can't access it outside of the while loop. Try this code.
<?php
include('config/dbconnect.php');
if(isset($_GET['id']))
{
$id = $_GET['id'];
$sql = mysqli_query($con, "SELECT * FROM collage WHERE id = '$id'");
while($row = mysqli_fetch_array($sql)) {
?>
<div class='body-content'>
<div class='img-name-cont'><h3><?php echo $row['name']; ?></h3></div>
<div class='img-detail-cont'>upload: <?php echo $row['date_upload']; ?><br>type:
<?php echo $row['type']; ?><br>size: <?php echo $row['size']; ?>KB </div>
<div class='img-file-cont'>
<img src="img/collage/<?php echo $row['file']; ?>" style="width:130px; height:100%"></div>
<div class='img-content-cont'><?php echo $row['content']; ?></div>
</div>
<?php
}
}
?>
Be sure to check echo $row['file'] if it returns the correct string of your image path with the correct file extension .
try this one :
first of all you have to check if your image path ok or not using this php function:
<?php
$img=getimagesize($imagewithpath);
if($img=="")
{ echo "there is no image ";}
else{ ?> <img src="<?php echo $imagewithpath; ?>" style="width:130px; height:100%"> <?php } ?>
As answered by Dulaj Sanjaya all your variables are local so you can't use that outside of while.
Second if you want to use your code then you have taken all data of $row in different variables then used outside of file then why don't you used same $file for image why you have used $row['file']?
Simply replace this line as
<div class='img-file-cont'>
<img src="img/collage/<?php echo $file; ?>" style="width:130px; height:100%"></div>
I have a products page where 8 product images are in a list which is being populated by images stored in a MySQL database. The images all have associated ID's in which a price, product name, and description is also associated with the same ID.
The idea behind what I am trying to do is; When a user clicks on one of the 8 product images, they will be redirected to a "checkout" page which will display the same image, plus all the information that is also stored under that ID in the database.
As of right now, I have the checkout page URL including the ID of the image (url.com/checkout.php?id=1) and I was hoping to find a way to get all the information stored under that ID in the URL to be displayed where called on the page.
Here is my php code that displays the images in the list on the products page:
// Grab the data from our template table
$sql = "select * from templates";
$result = mysql_query($sql) or die ("Could not access DB: " . mysql_error());
while ($row = mysql_fetch_assoc($result))
{
echo "<li>";
echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">";
// Note that we are building our src string using the ID from the database
echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />";
echo "<span>";
echo "<big>" . $row['name'] . "</big>";
echo $row['description'];
echo "</span>";
echo "</a>";
echo "</li>";
}
Here is the php code that is supposed to gather the information of the clicked product (but doesn't):
if (isset($_GET['id']))
$id=$_GET['id'];
else
$id=1;
if (isset($_GET['action']))
$action=$_GET['action'];
else
$action="empty";
switch($action){
case "add":
if($_SESSION['cart'][$id])
$_SESSION['cart'][$id]++;
else
$_SESSION['cart'][$id]=1;
break;
case "remove":
if($_SESSION['cart'][$id])
{
$_SESSION['cart'][$id]--;
if($_SESSION['cart'][$id]==0)
unset($_SESSION['cart'][$id]);
}
break;
case "empty":
unset($_SESSION['cart']);
break;
}
//Display Cart
if(isset($_SESSION['cart'])) {
$total=0;
foreach($_SESSION['cart'] as $id => $x) {
$result=mysql_query("select image,name,price,description from templates WHERE id=$id");
$myrow=mysql_fetch_array($result);
$image=$myrow['image'];
$name=$myrow['name'];
$price=$myrow['price'];
$description=$myrow['description'];
}
}
And here is the actual HTML/PHP where the information is supposed to be displayed:
<a class="caption" href="checkout.php">
<img src="http://URL-REMOVED.com/file_display.php?id=<?php $myrow['id'] ?>"/>
<span>
<big>
<strong><?php $myrow['name'] ?></strong>
</big>
<div class="price"><?php $myrow['price'] ?></div>
</span>
</a>
</div>
<div id="info_form_container">
<div class="product_info">
<div class="control-group">
<strong>Template Name:</strong>
<?php $myrow['name'] ?>
</div>
<div class="control-group">
<strong>Template Description:</strong>
<?php $myrow['description'] ?>
</div>
<div class="control-group">
<strong>Template Price: </strong>
<?php $myrow['price'] ?>
</div>
</div>
I guess I'm not really sure if this is even the best method to take? But I definitely want to have the images stored in the database and I definitely want to call them using the ID...
How can I achieve this? Where am I wrong in my code?
EDIT: I do not think I understood your question the first time. Try using this to generate your list of products, updating the connection information. If this works, use the methods below to sanitize your variables and store your connection information elsewhere
<?php
$mysqli_connection = new mysqli("localhost", "username", "password", "database");
$sql = "SELECT * FROM templates";
$result = $mysqli_connection->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$description = $row['description'];
echo '<li>';
echo '<a href="purchase.php?id='.$id.'" class="caption">';
echo '<img src="http://URL-REMOVED.com/file_display.php?id='.$id.'" />';
echo '<span>';
echo '<big>'.$name.'</big>';
echo $description;
echo '</span>';
echo '</a>';
echo '</li>';
}
?>
OG Answer:
I think what you are doing is just fine.. It is the method I use (although mine seems a little neater). Run your PHP functions to query your MySQL database using the ID you get, and just start dumping the information you get into your site. To help with readability and cut down on spelling confusions, it might help to assign your $myrow['whatever'] results into variables to echo out, but that is more cosmetic to me than anything.
To fix up your MySQL things and use mysqli, try out the following:
$sql = "SELECT * FROM templates";
$result = $mysqli_connection->query($sql);
while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
echo "<li>";
echo "<a class=\"caption\" href=\"purchase.php?id=\"" . $row['id'] . ">";
// Note that we are building our src string using the ID from the database
echo "<img src=\"http://URL-REMOVED.com/file_display.php?id=" . $row['id'] . "\" />";
echo "<span>";
echo "<big>" . $row['name'] . "</big>";
echo $row['description'];
echo "</span>";
echo "</a>";
echo "</li>";
}
And try to sanitize your information using:
$my_stuff = mysqli_connection->real_escape_string($row['that_stuff']);
Also, you know you can use single quotes ('') around your echo statements if you want to, right? May make it easier that escaping all of the double quotes..
So in full, this is probably a rough example of what I would do but I would split it up into functions and maybe create some global variables (such as the connection itself):
<?php
$mysqli_connection = new mysqli("localhost", "username", "password", "database");
$sql = "SELECT * FROM templates WHERE id=$id";
$result = $mysqli_connection->query($sql);
// I'm assuming there is only one entry, so no while loop for me
$row = $result->fetch_array(MYSQLI_ASSOC);
$your_title = $mysqli_connection->real_escape_string($row['title']);
$path_to_image = $mysqli_connection->real_escape_string($row['image']);
$description = $mysqli_connection->real_escape_string($row['description']);
$price = $mysqli_connection->real_escape_string($row['price']);
?>
<html>
<head></head>
<body>
<h3><?php echo $your_title; ?></h3>
<img src="<?php echo $path_to_image; ?>" />
<ul>
<li>Description: <?php echo $description; ?></li>
<li>Price: <?php echo $price; ?></li>
<!-- etc..-->
This code
<?php echo "Likes: ".$r['votes_up']." "; echo "Dislike: ".$r['votes_down'].""; ?>
Wont post the values from the table for 'votes_up' 'votes_down'
I cant get my head round this! Ive got this exact code working on a different page but it wont on this.
Heres the entire code ....
<div class="message">
<?php
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql)) {
$posted = date("jS M Y h:i",$r['posted']); echo "".$r['author']." $posted";?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo "".$r['message'].""; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2"><?php echo "".$r['message'].""; }?></div>
<?php echo "Likes: ".$r['votes_up']." "; echo "Dislike: ".$r['votes_down'].""; ?>
</div>
<br/>
<hr>
Can anyone help? its driving me insane
Line 8 of your code
<div class="message2"><?php echo "".$r['message'].""; }?></div>
Why is that closing curly brace in there before the closing ?> ?
You seem to have closed the WHILE loop here:
<div class="message2"><?php echo "".$r['message'].""; }?></div>
Notice the curly bracket.
Therefore, votes_up and votes_down have no values.
Your curly bracket is being placed before the last echo statement, therefore the $r variable is out of scope.
Move the } to later in your page like so
<div class="message">
<?php
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . mysql_real_escape_string($_GET['id']) . "'") or die(mysql_error());
while($r = mysql_fetch_array($sql))
{
$posted = date("jS M Y h:i",$r['posted']);
echo $r['author']." ".$posted;
?>
<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-text="<?php echo $r['message']; ?>">
Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>
<div class="message2">
<?php
echo $r['message'];
?>
</div>
<?php
echo "Likes: ".$r['votes_up']." ";
echo "Dislike: ".$r['votes_down'];
}
?>
</div>
<br/>
<hr>
Also notice the call to mysql_real_Escape_string in the $sql var. this will prevent nasty sql injections
This is because the $r['...'] variable is out of the scope where you place it.
Better way to do it (assuming you only fetch one row:
<?
$sql = mysql_query("SELECT * FROM threads WHERE id = '" . $_GET['id'] . "'") or die (mysql_error());
$row = mysql_fetch_assoc($sql);
?>
<div> ... </div>
<?php echo "Likes: " . $row['votes_up'] . " Dislike: " . $row['votes_down']; ?>