Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
For a school project I need to make a webshop. Right now the products are in a table, but I really dislike the look of it, because it looks very unprofessional. This is the code:
while($product = mysql_fetch_assoc($query)) {
echo "<form action=\"add_product.php\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"productnumber\" value=\"".$product['product_id']."\" />\n";
echo "<p> <b>".$product['name'] . "</b> <br />\n";
echo "<table>";
echo "<tr>";
echo "<td>";
echo "<td>" . $product['image'] . "</td>";
echo "<td>" . $product['description'] . "</td>";
echo "</tr>";
echo "</table>";
?>
<?php
echo "Price: €".$product['price']."<br />\n";
echo "Quantity: <input type=\"text\" name=\"Quantity\" size=\"2\" maxlength=\"2\" value=\"1\" />\n";
echo "<input type=\"submit\" value=\"Add To Cart\" /></p>\n";
echo "</form>\n";
}
}
?>
Now, from the template I'm using (Modus by Luis Zono) I want to make the products show up like this page: http://luiszuno.com/themes/modus/portfolio.html with this HTML code:
<div class="featured portfolio-list">
// This is one figure, but it can be copy-pasted to infinity:
<figure>
<img src="img/dummies/gallery-1.jpg" alt="Alt text" />
<div>
Pellentesque habitant morbi
tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.
</div>
<a class="link" href="project.html"></a>
</figure>
<div class="clearfix"></div>
</div>
My coding skills aren't very good, so how can I write code that makes the products in the loop look like the portfolio-figures? (so that the dummy image will be the $product['image'] and such). I'm totally stuck right now.
I'm not going to give the answer (SO is not that kind of site), but the road how to get there.The example below is IMO the best solution here, but for your situation:
In php you can do this:
while($product = mysql_fetch_assoc($query)) {
?>
<!-- Your code here -->
<img src="example-html-image.jpg" />
<!-- Your code here -->
<?php
} // end of while
So, instead of the current echo of html you got now, you add the html you want.
You probaly want to use the fetched variables in your code, below I'll provide two examples:
while($product = mysql_fetch_assoc($query)) {
?>
<!-- You can do this -->
<img src="image<?php echo $product['id']; ?>.jpg" />
<!-- Or the short version of echo -->
<img src="image<?=$product['id'] ?>.jpg" />
<?php
}
If you want to kick it up a notch, you should not use html in your code at all. The same way you dont use JS in-page, you dont use html in php. You use a template (this is the most proper method IMO):
<!-- example.html: -->
<div> Hi, Im product <!-- NAME --> </div>
Very simple, and I use a comment to get replaced further along the line.
Then you take php, open the file, and replace the comment:
$template = file_get_contents($_SERVER['DOCUMENT_ROOT'].'loc/to/file/example.html');
$template = str_replace("<!-- NAME -->", $product['name'], $template);
echo $template;
Assumed $product['image'] = "my_picture.jpg"
Then use this:
<img src="/path/to/images/<?php echo $product['image']; ?>" />
The result in html source will be:
<img src="/path/to/images/my_picture.jpg" />
Related
How can i clone the below HTML5 wrap. The code contains also some php serialized that must be in order , example Article one has <?php echo $price[n];?> where n must be a number from 0-15.
<!-- ARTICLE START -->
<div class="col-sm-6 col-md-4">
<article class="box has-discount">
<figure>
<a class="hover-effect popup-gallery" href=
"ajax/slideshow-popup.html"><img alt="" height="161" src=
"<php echo $img[0];?>" width="270"></a> <span class=
"discount"><span class="discount-text">VIP
DISCOUNT</span></span>
</figure>
<div class="details">
<span class="price"><small>avg/night</small> $<php echo $price[0];?></span>
<h4 class="box-title"><php echo $name[0];?>small><php echo $city[0];?></small></h4>
<div class="feedback">
<div class="five-stars-container" data-original-title=
"4 stars" data-placement="bottom" data-toggle="tooltip"
title="">
<span class="five-stars" style="width: 80%;"></span>
</div><span class="review">270 reviews</span>
</div>
<p class="description">Nunc cursus libero purus ac congue arcu
cursus ut sed vitae pulvinar massa idporta nequetiam.</p>
<div class="action">
<a class="button btn-small" href=
"hotel-detailed.html">SELECT</a> <a class=
"button btn-small yellow popup-map" data-box=
"48.856614, 2.352222" href="#">VIEW ON MAP</a>
</div>
</div>
</article>
</div>
<!-- ARTICLE END -->
I have a code that generates arround 20-30 hotels,
Each hotel has his own article and his own variable as $price[], $name[], etc were the [n] value in a number in ascending order starting from 0.
How can i generate the above div x how many hotels availeble and to insert the variable value automatic ?.
something like this? http://jsfiddle.net/swm53ran/173/
i simplified the code a bit and did everything in jquery, but i put notes on how to do it with php (i dont have readily available access to php editor) but the concept is the same.
<div class="hotel" id="template" style="display:none;">
<div class="name"></div>
<div class="price"></div>
</div>
$(document).ready(function() {
var hotels = [
{'name': 'hotel1', 'price':'$200'},
{'name': 'hotel2', 'price':'$300'},
{'name': 'hotel3', 'price':'$700'},
{'name': 'hotel4', 'price':'$100'}
];
for(var i = 0; i < hotels.length; i++) {
var clone = $('#template').clone(true).attr('id', '');
clone.css('display', '');
clone.find('.name').html('Name: ' + hotels[i]['name'] + '. With php should be something like < ? php echo $name[i]; ? >');
clone.find('.price').html('Price: ' + hotels[i]['price'] + '. With php should be something like < ? php echo $price[i]; ? >');
clone.appendTo('body');
}
});
you'd get the hotels array from php (im assuming) and then you can put php right into the html of the clone if you take out the spaces, then use i as the incrementor from the for loop. hope this helps
Recently I've set it up so I can update my sites news via my administration panel and now I've been seeking across the web for a solution to print specific lines.
$query = "SELECT * FROM BananzaNews";
$result = mysql_query("SELECT * FROM BananzaNews", $con);
$num_rows = mysql_num_rows($result);
The above tells me how many rows I have and for my five most recent which will be shown on my home page, I want to fetch the data for $num_rows, $num_rows -1, $num_rows -2, $num_rows -3 and $num_rows -4.
The data to fetch;
title
newsDate
imagePath
content
websitePath
My current dummy filled code is the following dubplicated five times;
<div class="Feed">
<div class="Img"><img alt="" src="http://www.davidreneke.com/wp-content/uploads/2013/12/News-Briefs.png"></div>
<div class="Title">My Testing Title</div>
<div class="Date">
<div class="Day">22</div>
<div class="Month">Jan</div>
<div class="Year">2015</div>
</div>
<div class="Brief">Vivamus consectetur et sapien vel rhoncus. Maecenas gravida posuere hendrerit. Duis lobortis justo ac justo malesuada commodo. Proin iaculis, erat ac aliquet eleifend, neque felis tristique turpis, at dictum tellus lectus et lorem. Nunc turpis enim, auctor sed purus nec.</div>
<div class="More"></div>
<div class="Divider"></div>
</div>
My PHP
<?php
$query = 'SELECT title,newsDate,imagePath,content,websitePath FROM BananzaNews ORDER BY id Desc LIMIT 5';
$resultSe = mysql_query($query, $con);
if (mysql_num_rows($result) > 0) {
while($row = $resultSet->fetchRow ( DB_FETCHMODE_OBJECT )){
echo "<div class=\"Feed\">
<div class=\"Img\"><img alt=\"\" src=\"" $row->imagePath "\"></div>
<div class=\"Title\">" $row->title "</div>
<div class=\"Date\">
<div class=\"Day\">" $row->newsDate "</div>
<div class=\"Month\">" $row->newsDate "</div>
<div class=\"Year\">" $row->newsDate "</div>
</div>
<div class=\"Brief\">" $row->content "</div>
<div class=\"More\">" $row->websitePath "</div>
<div class=\"Divider\"></div>
</div>";
}
}
?>
Database View
You want to use MySQL's ORDER BY. You don't need to use num_rows to get the 5 latest posts. Simply order them by their id columns and then LIMIT it to 5. If they don't have id columns, they should have an id column that auto_increments and is a PRIMARY KEY. With that, this code will help you:
//Load your mysql_query into a variable SELECTing not *, but only the rows you need.
$query = mysql_query("SELECT title, newsDate, imagePath, content, websitePath FROM BananzaNews ORDER BY newsid DESC LIMIT 5") or die(mysql_error()); //<- Error handling.
//This loads up each row into an associative array into the variable $row
//So, $row['title'] will be equal to the 'title' column at that row in your database
while($row = mysql_fetch_array($query)){
//Output your repeating code
//Notice how I've used concat operators (dots) to stop the echo and start it again.
//I'd recommend looking those up if you haven't.
echo
'<div class="Feed">
<div class="Img"><img alt="" src="'.$row['imagePath'].'"></div>
<div class="Title">'.$row['title'].'</div>
<div class="Date">
<div class="Day">22</div>
<div class="Month">Jan</div>
<div class="Year">2015</div>
</div>
<div class="Brief">'.$row['content'].'</div>
<div class="More"></div>
<div class="Divider"></div>
</div>';
}
I would actually also use mysqli over mysql, mainly because it's well... mysql-improved. Do look into it, I highly recommend it. Even better, use PDO.
I hope this was helpful!
if you want to get the data to fetch run your query in while loop
$result=mysqli_query("select * from table_name order by id desc limit 0,5")
while($each=mysqli_fetch_array($result ))
{
//your code goes here
}
Use this query it will work for you
$query = 'SELECT title,newsDate,imagePath,content,websitePath FROM BananzaNews ORDER BY id Desc LIMIT 5';
$resultSet = mysql_query($query);
if (mysql_num_rows($query) > 0) {
while($row = mysql_fetch_array($resultSet)){
echo $row['title'];
echo $row['newsDate'];
}
}
You can loop your data like this.
Try this it will work :
1. use mysqli or pdo instead of mysql extension. It already depreciated and will be remove in future.
2. Loop your result set to get all the data from the table.
<?php
$result = mysqli_query($con,"SELECT * FROM BananzaNews LIMIT 0,5");
$rs = mysqli_fetch_array($result);
?>
<table>
<?php
do
{
?>
<tr>
<td><?php echo $rs['title']; ?></td>
<td><?php echo $rs['newsDate']; ?></td>
<td><?php echo $rs['imagePath']; ?></td>
<td><?php echo $rs['content']; ?></td>
<td><?php echo $rs['websitePath']; ?></td>
</tr>
<?php
}while($rs = mysqli_fetch_array($result));
?>
</table>
I'm a beginner so this may ba a stupid one.
I'm creating simple platform, which uses a database to manage my post stamps collection. I've managed to set up a database and gather new stamps from user. Now I want to create kind of "product page", which will use data from mySQL. You will be able to enter this page through a dynamically generated table, which will contain all stamps in the database.
The code,which generates a simple table is here:
//connect to database
$db = mysqli_connect(database login data);
if(mysqli_connect_errno()){
echo "Jest problem z podłączeniem się do bazy danych. Skontaktuj się z administratorem.";
die();
}
// loop through results of database query, displaying them in the table
$result = mysqli_query($db,"SELECT * FROM stamps");
?>
</head>
<body>
<?php include "navbar.php" ?>
<h1>Przeglądaj swoją kolekcję.</h1>
<?php
echo "<p><b>View All</b>";
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>ID</th> <th>Nazwa</th> <th>Kraj</th> <th>Wartość nominalna</th> <th>Wartość rynkowa</th> <th>Klaser</th> <th>More info</th> <th>Edytuj</th> <th>Usuń</th></tr>";
while($row = mysqli_fetch_object($result)) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row->id . '</td>';
echo '<td>' . $row->name . '</td>';
echo '<td>' . $row->country . '</td>';
echo '<td>' . $row->price . '</td>';
echo '<td>' . $row->estimated . '</td>';
echo '<td>' . $row->album . '</td>';
echo "<td>More info.</td>";
echo '<td>Edit</td>';
echo '<td>Delete</td>';
echo "</tr>";
}
// close table>
echo "</table>";
?>
Now, I'd like to post database values to another page, after clicking "More info" and just view it something like that:
<body>
<?php include "navbar.php" ?>
<div class="jumbotron">
<img class="photo" src="http://placehold.it/250x250">
<h1>Znaczek z XIX wieku</h1> <!-- database data goes here -->
<h3>Informacje:</h3> <!-- database data goes here -->
<h5>Numer katalogowy:</h5> <!-- database data goes here -->
<h5>Rok wydania:</h5> <!-- database data goes here -->
<h5>Kraj:</h5> <!-- database data goes here -->
<h5>Wartość nominalna:</h5> <!-- database data goes here -->
<h5>Wartość rynkowa:</h5> <!-- database data goes here -->
<h5>Klaser:</h5> <!-- database data goes here -->
<h3>Opis:</h3> <!-- database data goes here -->
<h5><div>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Ipsa, aliquam, tenetur voluptatibus veritatis numquam expedita nesciunt quos repudiandae similique atque provident ipsam dolorem recusandae id possimus minus ea eum cupiditate!</div>
<div>Sint, debitis, saepe repellendus commodi recusandae error architecto voluptates soluta ipsa facere perferendis aspernatur quo atque fugiat consequatur! Assumenda necessitatibus dolorem esse distinctio incidunt pariatur praesentium veniam voluptate quae quaerat!</div>></h5>
<p><a class="btn btn-primary btn-lg" role="button">Edytuj</a></p>
</div>
<?php include "footer.php" ?>
</body>
The question is: what is the best way to do it ?
Thanks in advance !
For generating page dynamically you can use $_GET['id'] for getting the id of the table, from where you are gonna fetch the data. Now put a get query after the link you wanna use,
It should look like this
Index.php?id=2
It is gonna fetch all the data from the second row.
But if you are like making a admin board or anything that is gonna modify the database do not use this, use secure login with user password verification,
Be sure to sanitize the id as this is going to used for getting the data in the database.
I have 3 web pages with the same code within a website. I am having success with 2 of my 3 pages. The ckeditor instances update as expected.
I have finally figured out "what the issue is", BUT still don't know how to fix it. It seems that if I type only one line of code and click out of the div (ie. blur event happens) it saves as is expected. If I hit a hard return and type other text the blur event won't save ANYTHING after the hard return. Seems to be a bug in this version of CKEDITOR. As I mentioned, I have 2 other pages with exactly the same code and everything works just fine.
<?php
session_start();
$thisPage = "services";
require('functions.php');
include('header.htm');
?>
<title>Services | Dr. Your Name</title>
<style type="text/javascript">
#cke_body {margin-left:120px;margin-top:30px;width:520px;background-color:gray;}
</style>
<script type="text/javascript">
CKEDITOR.config.filebrowserBrowseUrl= 'browser/browseAdminUploads.php';
CKEDITOR.config.extraPlugins = 'justify';
</script>
</head>
<body>
<div id="header" class="clear">
<div id="headerContent">
<?php include ("bannerIcons.php"); ?>
<div id="logo">
<img src="images/logo.png" title="home" alt=""/>
</div> <!--end logo-->
<?php include('mainMenu.php');?>
</div>
</div> <!--end header-->
<div id="container">
<div id="content" class="shadow"><div class="content">
<div id="colLt"><div class="content">
<?php
connect();
$sql = mysql_query("SELECT services FROM contentAreas") or die("nothing found");
$row = mysql_fetch_assoc($sql);
if ($_SESSION['username']=='admin'){
echo "<div id='services' contenteditable='true' onblur='saveServices()'>";
} else {
echo "<div id='services'>";
}
echo $row['services'];
echo "</div>";?>
<script type="text/javascript">
function saveServices() {
var data = CKEDITOR.instances.services.getData();
$.post('saveServices.php', {services:data})
}
</script>
</div></div>
<?php include('saveServices.php');?>
<div id="colRt"><div class="content">
<div id="serviceBox" class="shadow"><div class="content">
<p class="big italTxt">one or more testimonials could go here. Lorem ipsum dolor sit amet, mel cu atqui perfecto, nec te vero fugit denique, an vel mundi tritani concludaturque.<br><br>Laoreet erroribus eos no. Eu nec maluisset repudiandae. Possit lucilius constituam his cu, quas liber sea an, eum purto errem audire eu. In viris assentior vis, pri iudico dolorem electram ne, ea ius scripta virtute.</p>
</div></div>
</div></div><!--end colRt-->
</div></div><!--end content-->
<div id="footer">
<?php include("footer.htm") ?>
</div><!--end footer-->
<div class="clear"></div>
</div><!--end container-->
<div class="clear"></div>
</body>
</html>
My file saveServices.php is as follows:
<?php
$services=$_POST['services'];
echo "hello<br>";
echo "services: ".$services;
include('functions.php');
connect();
$sql1 = mysql_query("UPDATE contentAreas SET services = '$services'") or die ("Your information has not been posted");
?>
Thanks again for your help!
I can't believe this nor do I understand it, but when I changed my div name from "services" to "offers", it worked. Is services a reserve word? Could my function name saveServices() be too long? I would love to hear a concrete explanation about why this has happened.
Also, I upgraded from 4.0.2 standard to 4.2.2 standard.
I am trying to do something easy I am sure but I have looked and tested and I am not doing something right.
I have a DB that stores the image file name, I need to get the file name based on the ID in the HTML , let me explain:
<div class="slide">
<div class="image-holder">
<img src="img/asoft_table.jpg" alt="" />
</div>
<div class="info">
<p>Morbi a tellus lorem, id scelerisque ligula. Maecenas vitae turpis et.</p>
</div>
</div>
<div class="slide">
<div class="image-holder">
<img src="img/soft_table.jpg" alt="" />
</div>
<div class="info">
<p>Sed semper, lorem ac lobortis bibendum, magna neque varius augue, vel accumsan.</p>
</div>
</div>
<div class="slide">
<div class="image-holder">
<img src="img/living_room2.jpg" alt="" />
</div>
in each instance of an img tag, I need to insert the filename from the DB. so, first image tag would be primary key 1, second primary key 2 and so forth.
Here is the PHP script I am using to retrieve the filename, which works, but I am unsure how to send the ID of the image to the script and then return it properly.
<?php
$hote = 'localhost';
$base = '*****';
$user = '*****';
$pass = '*****';
$cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
$ret = mysql_select_db ($base) or die (mysql_error ());
$image_id = mysql_real_escape_string($_GET['ID']);
$sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
$result = mysql_query($sql);
$image = mysql_result($result, 0);
header('Content-Type: text/html');
echo '<img src="' . $image . '"/>';
exit;
?>
any help would be appreciated, thanks a heap
From what it looks your trying too hard to separate the PHP from HTML.
// File: index.php
<?php
$hote = 'localhost';
$base = '*****';
$user = '*****';
$pass = '*****';
$cnx = mysql_connect ($hote, $user, $pass) or die(mysql_error ());
$ret = mysql_select_db ($base) or die (mysql_error ());
$image_id = mysql_real_escape_string($_GET['ID']);
$sql = "SELECT image FROM image_upload WHERE ID ='$image_id'";
$result = mysql_query($sql);
//$image = mysql_result($result, 0);
$image = array();
while ($row = mysql_fetch_assoc($result)) {
$image[] = $row["image"];
}
?>
<html>
<head>
</head>
<body>
<div class="slide">
<div class="image-holder">
<img src="<?php echo $image[0];?>" alt="" />
</div>
<div class="info">
<p>Morbi a tellus lorem, id scelerisque ligula. Maecenas vitae turpis et.</p>
</div>
</div>
<div class="slide">
<div class="image-holder">
<img src="<?php echo $image[1];?>" alt="" />
</div>
<div class="info">
<p>Sed semper, lorem ac lobortis bibendum, magna neque varius augue, vel accumsan.</p>
</div>
</div>
<div class="slide">
<div class="image-holder">
<img src="<?php echo $image[2];?>" alt="" />
</div>
</body>
</html>
// This is the magic code to get all the rows out of the database :)
// $row[ field_name ];
while ($row = mysql_fetch_assoc($result)) {
$image[] $row["image"];
}
Edit:
I'm not sure if this is what your trying to accomplish, but thought I'd share anyway.
$imageID1 = $_GET['id1'];
$imageID2 = $_GET['id2'];
$imageID3 = $_GET['id3'];
$sql = "SELECT image FROM image_upload ";
$sql = "WHERE ID = $imageID1 OR ID = $imageID2 OR ID = $imageID3";
//The rest of your code can remain the same.
Or if one id relates to 3 images.
$sql = "SELECT * FROM image_upload WHERE ID ='$image_id'";
$result = mysql_query($sql);
$image = array();
$row = mysql_fetch_assoc($result);
$image1 = $row["image1"];
$image2 = $row["image2"];
$image3 = $row["image3"];
I've you give me more info on what your trying to do, I'd be happy to give you a better example.
Make sure your HTML file is actually a php file. Put the PHP code at the top of it then put your HTML below it. You can use
<?php echo $image; ?>
to put the variable in like this:
<div class="slide">
<div class="image-holder">
<img src="<?php echo $image; ?>" alt="" />
</div>
<div class="info">
<p>Sed semper, lorem ac lobortis bibendum, magna neque varius augue, vel accumsan.</p>
</div>
</div>
It's always a good idea to use htmlentities for security (avoids javascript injection):
<img src="<?php echo htmlentities($image, ENT_QUOTES, "UTF-8"); ?>" alt="" />
Since you are using the $_GET array, you will want to send the ID through the URL, for instance:
http://www.example.com/image.php?ID=5
That is assuming you want to continue using $_GET for this script.