Printing Horizontal Tables Including Images Using PHP MySQL - php

I am trying to print out on my sites home page a horizontal table which includes a picture of a car and then below the make, model & price of the car.
Here's roughly what I have done:
<?php
$List = "";
$sql = mysql_query("SELECT * FROM Car ORDER BY listed DESC LIMIT 5");
$Count = mysql_num_rows($sql);
if ($Count > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$make = $row["make"];
$model = $row["model"];
$price = $row["price"];
$List .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="20%" valign="top"><a href="/motors/cars.php?id=' . $id . '"><img style="border:#666 1px solid;"
src="/../../motors-' . $id . '.jpg" alt="' . $status . ' ' . $title . '" width="100" height="80" border="1" /></a></td>
</tr>
<tr>
<td width="80%" valign="top" align="left"><font face="Arial" color="#000080"><B>
' . $make . ' ' . $model . '</B></font><br />' . $price . '
view car details</align></td>
</tr>
</table>';
}
} else {
$List = "No cars at present";
}
mysql_close();
?>
Can anyone help me sort this code to print out horizontally? Many Thanks!

The src of the image is most likely wrong, /../../motors... is still /motors...
you can move the images into your webapps /motors/images folder and set src as /motors/images/motors...
The table you create must be nested in another table.
<table>
<tr>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
<td>
<table of a car>
</td>
.... etc.
</tr>
</table>
It will make sense to emit a tr every few cars to wrap to a new line.
table of a car is the html you collect in $List.

Related

Get a while loop to run "X" number of times with two conditions in PHP

So i have this code:
<?php
require_once('../../ccconnect.php');
$query = "SELECT * FROM orgs";
$response = #mysqli_query($dbc, $query);
if($response){
echo '<table align="left" cellspacing="5" cellpadding="8">
<tr>
<td align="left"> <b>Logga</b> </td>
<td align="left"> <b>Namn</b> </td>
<td align="left"> <b>Andel till ändamål</b> </td>
<td align="left"> <b>Vinst</b> </td> </tr>';
while($row = mysqli_fetch_array($response))){
echo '<tr>
<td align="left">' . $row['logo'] . '</td>
<td align ="left">' . $row['name'] . '</td>
<td align ="left">' . $row['chare'] . '</td>
<td align ="left">' . $row['profit'] . '</td>';
echo '</tr>';
}
echo '</table>';
} else {
echo "Kunde inte genomföra databaskomandot<br>";
echo mysqli_error($dbc);
}
?>
The code will print out a table of results from my database, but i want to limit the results i get to only show 5 results for example. How would i do this?
Use the LIMIT clause in SQL.
SELECT * FROM my_table
LIMIT 5;
https://dev.mysql.com/doc/refman/5.5/en/select.html
http://www.w3schools.com/php/php_mysql_select_limit.asp
You need to limit the query or limit the loop. (I like the first option better).
You have this query:
$query = "SELECT * FROM orgs";
Wanna get 5 results only?
$query = "SELECT * FROM orgs LIMIT 5";

How to display multiple rows one after the other repeating a sequence depending on how many rows I get?

My idea is to create a Q&A section under a product profile, just like on eBay or Amazon or whatever. The idea is to send a question and then get the owner of the article to reply.
The table has these columns: pid (product ID), id (question ID), question, answer, date (date posted), username.
So if I post a question, I get the ID of the product in which I'm posting and create a question. The the owner just sends the answer to the row that matches the question.
Here's my PHP code to retrieve all the info from that table:
$qanda = '';
$link = mysql_connect("localhost", "youknowwhat", "youknowwhat");
mysql_select_db("youknowwhat", $link);
$qandaq = mysql_query("SELECT * FROM questions WHERE id='$id2' ORDER BY date", $link);
$count = mysql_num_rows($qandaq);
if($count >= 1){
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
}
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
} else {
$qanda = '<div id="answers" align="center">
No questions for this product.
</div>';
}
Now... what you see as a table in the variable $qanda I want to repeat it over and over again but displaying different row data but the concatenation isn't working and I can only get the last row to be displayed. I just can't seem to find out why this isn't working! Am I missing something?
All you have to do is append your divs (.=) while you're inside the while loop that mysql_fetch_array() rows.
Then you'll have a new div for each row your database returns, and you can populate it easily.
$qanda = '';
while($rows = mysql_fetch_array($qandaq)){
$date = $rows['date'];
$q = $rows['question'];
$a = $rows['answer'];
$usrname = $rows['username'];
$qanda .= '<div id="answers" align="center">
<table cellspacing="0" align="center">
<tr align="center">
<td width="200">' . $date . '</td>
<td rowspan="2" width="400"><strong>' . $q . '</strong><br>' . $a . '</td>
<td width="200">Delete</td>
</tr>
<tr align="center">
<td>' . $usrname . '</td>
<td>Report</td>
</tr>
</table>
</div>';
}

How do I pass values with a link to a new page?

What I want is to be able to click on the link (review.php) and then to display the review corresponding to that film only, on a new page.
Not sure if my MySQL query is wrong or if my A href link is not formatted correctly.
Any help would be appreciated.
require_once('./includes/mysql_connect.php');
$query = "SELECT films.movie_title, films.rating, films.actor, reviewed.review
FROM films
INNER JOIN reviewed
ON films.movie_id=reviewed.review_id";
$result = mysql_query($query) or die ("Could not execute mysql" . mysql_error()); // Run Query
$num = mysql_numrows($result);
if ($num > 0) { // If it ran ok, display records.
echo "<p> There are curently $num records.</p>";
// Table header.
echo '<table border="1" align="center" cellspacing="0" cellpadding="5">
<tr>
<td align="left"><b>Movie Title</b></td>
<td align="left"><b>Leading Actor</b></td>
<td align="left"><b>Rating</b></td>
<td align="left"><b>Review</b></td>
</tr>';
// Fetch and print all the records.
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<tr>
<td align="left">' . $row['movie_title'] . '</td>
<td align="left">' . $row['actor'] . '</td>
<td align="left">' . $row['rating'] . '</td>
<td align="left"><a href="review.php?id='. $row['review'] . '> Read Review </a>
</tr> ';
}
}
?>

Style with css in php

I'm new to this so please be gentle
I want to style for example $product_name, $author, $details.
I guess I could style the echos but in this case the only echo is
<?php
// Connect to the MySQL database
include "storescripts/connect_mysql.php";
// This block grabs the whole list for viewing
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY id DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$author = $row["author"];
$details = $row["details"];
$link = $row["link"];
$isbn = $row["isbn"];
$isbn13 = $row["isbn13"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="580" border="0" cellspacing="0">
<tr>
<td width="120"><img src="/inventory_images/' . $id . '.jpg" width="120" height="184" border="0" /></td>
<td width="456"><p>' . $product_name . '<p><br />
<p>' . $author . '</span><br />
<p>' . $details . '<br />
visa </td>
</tr>
</table>';
}
} else {
$dynamicList = "Database empty.";
}
mysql_close();
?>
Have you tried adding some CSS classes to your table cells?
$dynamicList .= '
<table width="580" border="0" cellspacing="0">
<tr>
<td width="120"><img src="/inventory_images/' . $id . '.jpg" width="120" height="184" border="0" /></td>
<td width="456"><p class="product_name">' . $product_name . '<p><br />
<p class="author">' . $author . '</span><br />
<p class="detail">' . $details . '<br />
visa </td>
</tr>
</table>';
In the head add some styles:
<style>
.product_name{color:red;}
.author{color:green;}
.detail{color:blue;}
</style>
Why not just add CSS rules to your stylesheet?
table tr td p {
// some styles
}
You can add a class with styles to the <p> elements that contain each value
<td width="456"><p class="product-name">' . $product_name . '</p>...
PHP is only a preprocssor and coughs up the code as the page loads and before any of the css of HTML kicks in so you can style it as normal
You can use style="" and class="" parameters on your <p> paragraph tags as well on the table cells <td>. You can also try to add for example styled <span> or <div> tags to your variables I suppose.
For example:
$product_name = "<span class='yourstyleclass'>$row['product_name']</span>";

creating a new array every 10 items added

I have the following code that shows the items added in the producs table in the database and i want to make it every 10 items creating a new array so that it won't be a long list of items.
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
while($row = mysql_fetch_array($sql)){
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="100%" border="0" cellspacing="0" cellpadding="6">
<tr>
<td width="10%" valign="top"><img style="border:#666 1px solid;" src="inventory_images/' . $id . '.jpeg" alt="' . $product_name . '" width="93" height="102" border="1" /></td>
<td width="90%" valign="top">' . $product_name . '<br />
€' . $price . '<br />
<a class="provoli" href="product.php?id=' . $id . '">Προβολή Λεπτομεριών</a></td>
</tr>
</table>';
Your code-style could use some work, as it is not very pretty, but, to make this work, you could try putting your list in an array.
This requires two steps, the first is keeping record of an index. The second is adding the values to your new array.
$index = 0;
while(<your condition>) {
$index++;
<more code>
$list[((int)$index/10)] .= <string>
}
A better way is to implement this when printing the array with all your items. But I don't know how you're implementing this further.

Categories