The user on my site can currently search a mysql db via PHP, and the results are displayed on the same page inside a DIV, thanks to ajax...
What I need now is to display an image that is associated with the mysql results...
Say the result has ID=250, then I want a piece of code to search in a folder for an image with the name 250.jpg on the server...
And then display this image in a table column using php script...
Here is my php script that displays the results as well as where I want the picture to appear...
Please help me...
$qry_result = mysql_query($query) or die(mysql_error());
}
// Build Result String
$display_table = "<table align='center'>";
// Insert a new row in the table for each result
while($row = mysql_fetch_array($qry_result)){
$display_table .= "<tr>";
$display_table .= "<td class='ad_container' colspan='4'></td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td width='110' rowspan='2'>----- IMAGE HERE -----</td>";
$display_table .= "<td width='377' height='15'>$row[headline]</td>";
$display_table .= "<td width='67' rowspan='2'>$row[insert_date]</td>";
$display_table .= "</tr>";
$display_table .= "<tr>";
$display_table .= "<td height='15'>$row[price]:-</td>";
$display_table .= "</tr>";
}
$display_table .= "</table>";
echo $display_table;
This solution checks to ensure the image actually exists prior to display:
// absolute path to image directory
$path = '/var/www/vhosts/path/to/dir';
// basepath image directory
$basepath = '/path/to/dir';
// assuming you store JPEGs
$image = $row['id'] . '.jpg';
// start column output
$display_table .= '<td width="110" rowspan="2">';
// make sure image exists
if (file_exists($path . $image)) {
$display_table .= '<img src="' . $basepath . $image . '" />';
}
// end column output
$display_table .= '</td>';
I think something like this would work:
$display_table .= "<td width='110' rowspan='2'><img src='/image/folder/" . $row["id"] . ".jpg'/></td>";
Related
I am trying to make a table that has different colors for each of its columns. There will be four columns.
$colors = array("background-color:red;", "background-color:gold", "background-color:pink;", "background-color:purple;");
$html = '<table>';
foreach( $array as $key=>$value){
$html .= '<tr style="background-color:white;">';
foreach($value as $key2=>$value2){
$html .= '<td>' . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
I created an array called colors that has the strings of the colors I want, but I don't know how to put that into the tag. I tried typing it in there, but since it is a string, it takes it as text instead of as code. Where it says "background-color:white;", I'd like it to call the values from the array instead. Thanks.
You can array_pop for this provided you have exactly 4 columns. You also can't apply background colours to <tr> like that, you need to apply them to the <td>
$colors = array("background-color:red;", "background-color:gold", "background-color:pink;", "background-color:purple;");
$html = '<table>';
foreach( $array as $key=>$value){
$color = array_pop($colors);
$html .= "<tr>";
foreach($value as $key2=>$value2){
$html .= "<td style='{$color}'>" . htmlspecialchars($value2) . '</td>';
}
$html .= '</tr>';
}
I have a php file that creates a table for me using data fetched from a mysql database.
<?php
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['CountryCode'] . "</td>";
echo "<td>" . $row['District'] . "</td>";
echo "<td>" . $row['Population'] . "</td>";
echo "<td>" . $row['Price'] . "</td>";
echo "<td><input type=\"number\" name="quantity" id="quantity"></td>";
echo "</tr>";
}
} else {
echo "0 results";
}
$conn->close();
?>
At the end of each row, I have an input number so that I can calculate the amount of each product(countries in this case). How could I get the amount of each country, multiply it by the price and return a total at the end of my table?
If you want to do it with PHP, you have to do it in two steps, because the PHP is processed in the server. So you would enter all the quantities you want and when you press the "Compute" button, the page would send all the values to the server who would compute everything you want and give you the final result.
A problem that I see with this solution is that once you have get all the quantities you want, you would have to get the prices once more. I don't its very efficient because you already have them on the first page. I see two solutions here :
When you display the values, keep track of the prices in a $_SESSION field, thus you will be able to access them after leaving the page, without having to get them from the database.
When you display the values, display the prices in an input field (I would recommend as readonly so that the user won't change this value here), and thus they would be included in the $_POST (or $_GET, if you use this) variable that your browser would send to the server.
If you're only interested to display the value, without using it after, you can use javascript to compute it, it might be easier.
Even though it's not the point here, you should be careful when displaying inputs via PHP on a while loop, because here all of your inputs have the same id, which would make thing harder to compute what you want to.
Hope it helps!
You must add a form tag around the table and then sum record price * qty on every row
What I did here is I checked if the quantity is set in the POST variable or not. if it exists I put data in quantity value. after that, you must make the name of quantity capable of taking array inside with the key of record Id.
after that, if the form submitted the quantity will send back to the page as a query string. then I multiply the price with quantity and add it to sum varaible. after finishing the loop and echo out the sum variable at the end of the table.
$html = '';
if ($result->num_rows > 0) {
$html .= '<form method="GET" action="" target="_self>';
$row = $result->fetch_assoc();
if(isset($_GET['quantity'][$row['ID']]))
$qty = $_GET['quantity'][$row['ID']];
else
$qty = 0;
$sum = 0;
// output data of each row
while ($row) {
$html .= '<tr>';
$html .= '<td>' . $row['ID'] . '</td>';
$html .= '<td>' . $row['Name'] . '</td>';
$html .= '<td>' . $row['CountryCode'] . '</td>';
$html .= '<td>' . $row['District'] . '</td>';
$html .= '<td>' . $row['Population'] . '</td>';
$html .= '<td>' . $row['Price'] . '</td>';
$html .= '<td>
<input type="number" name="quantity[' . $row['ID'] . ']" value="' . $qty . '">
</td>';
$html .= '<td>' . ($sum += $row['Price'] * $qty) . '</td>';
$html .= '</tr>';
}
$html .= '<input name="submitForm" value="submitForm">';
$html .= '</form>';
$html .= '<tr>';
$html .= '<td>Total = '.$sum.'</td>';
$html .= '</tr>';
} else {
$html .= "0 results";
}
$conn->close();
echo htmlspecialchars($html);
remember to use htmlspecialchars to echo out the result.
I'm, trying to get the Joomla topbanner to show 2 different images leading to 2 different links, so I changed the following code,
from this:
// get a parameter from the module's configuration
$imgname = $params->get('imgname');
$imgwidth = $params->get('imgwidth', '');
$imgheight = $params->get('imgheight', '90');
$imgtarget = $params->get('imgtarget', '');
$imgpath = JURI::base().'images/stories/';
$html = "<a href='".$imgtarget."' target='_blank'>";
$html .= "<img src='".$imgpath.$imgname."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
echo $html;
to this:
// get a parameter from the module's configuration
$imgname = $params->get('imgname');
$imgwidth = $params->get('imgwidth', '');
$imgheight = $params->get('imgheight', '90');
$imgtarget = $params->get('imgtarget', '');
$imgpath = JURI::base().'images/stories/';
//split up image and targets
list($image1,$image2) = explode(',',$imgname);
list($target1,$target2) = explode(',',$imgtarget);
//steps
//divide image into 2 using any image editing tool
//upload to server
//set up images separated by comma in admin
//add second target to admin separated by comma
$html = "<a href='".$target1."' target='_blank'>";
$html .= "<img src='".$imgpath.$image1."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
$html = "<a href='".$target2."' target='_blank'>";
$html .= "<img src='".$imgpath.$image2."' border='0' alt='' width='".$imgwidth."' height='".$imgheight."'>";
$html .= "</a>";
echo $html;
I edited it in the back end with a delimiter but it is only reading the second image and link.
Any help please?
The issue is with this line:
$html = "<a href='".$target2."' target='_blank'>";
You're not cocatenating $html, you're reassigning it. All the old information is lost. Simply do this:
$html .= "<a href='".$target2."' target='_blank'>";
I know there are lots of plugins there to display a table with static headers. My issue is that i have a for loop which returns the values under this table:
foreach($ftsProducts as $x) {
$content .= "<tr class='highlight'>
<td class='reportCell' title='" . $x['BaseCode'] . "'>" . $x['Description'] . "</td>
<td class='reportCell'>" . $x['Fit'] . "</td>";
foreach($sizes as $y) {
$content .= "<td class='reportCell'>" . $x[$y] . "</td>";
}
$content .= "<td style='font-weight: bold; text-align: center;'>" . $x['Total'] . "</td>
</tr>";
}
$content .= "</table>";
Anyone got any suggestions or advice.
Many thanks
As far as i understand, your problem is not related to PHP, but to HTML and CSS. Static headers are tricky to achieve and there are probably browsers, which won't let you do them. Have a look at this solution, for example:
http://www.imaputz.com/cssStuff/bigFourVersion.html
By viewing the source of the page, it should -- imo -- be obvious how you have to build your table with PHP.
HTH
I am outputing 4 columns from a mysql query, but using the code below doesnt align each column wiht the headers, I guess is due the fact this are static declared against the dynamic rows . Can someone advise a way to align the headers properly with each fetched column ,,,
$tableStyle = "padding: 5px;border:1px";
$tdStyle = "padding:5px ";
$thStyle = "padding:5px; align:center ";
echo '<table style="' . $tableStyle . '" cellpadding="7" cellspacing="7">';
echo "<tr> <th>Quiz Title </th><th> Score </th><th>Maximum Score </th><th>Finished On </th></tr>";
$row = $database->loadRowList();
foreach($row as $valuearray)
{
echo '<tr style=" align="center">';
foreach($valuearray as $field)
{
echo "<td>$field</td>";
}
echo "</tr>";
}
echo "</table>";
This line is wrong:
echo '<tr style=" align="center">';
I think you want:
echo '<tr style="text-align:center;">';
Are you using Joomla? The loadRowList() suggests this. If you are, then use loadAssocList() instead, which returns the field names as well as the field values. You can do then a separate loop to output your column headers and guarantee they match up with the data fields.
docs for the function here: http://help.joomla.org/content/view/509/60/
you'd do something like:
$rows = $database->loadAssocList();
echo '<tr>';
foreach(array_keys($rows[0]) as $header) {
echo "<th>$header</th>";
}
echo '</tr>';
foreach ($rows as $row) {
echo '<tr>';
foreach($row as $value) {
echo "<td>$value</td>";
}
echo '</tr>';
}