php code not working inside html - php

I am trying to display an image. I have fetched my URL from the db storage. And i have used the php variable inside the image tag. But the code does'nt display any image .
what is the problem? exactly!
this is my code below
<?php $db =& JFactory::getDBO();
$query88=$sql = "SELECT file_url_thumb FROM fs01_virtuemart_medias WHERE virtuemart_media_id=1 LIMIT 0, 30 ";
$result88 = mysql_query($query88) or die(mysql_error());
?><img src="<?php while($row = mysql_fetch_array($result88)){
echo $row['file_url_thumb'];
echo "<br />";
} ?>" border="0" style="border: 0; vertical-align: top;" />

You are looping over your results and putting them all (each followed by a <br /> inside the src attribute of an img tag. It seems highly unlikely that that won't 404.
You probably want something more like:
<ul>
<?php while($row = mysql_fetch_array($result88)){ ?>
<li><img src="<?php echo htmlspecialchars($row['file_url_thumb']); ?>" /></li>
<?php } ?>
</ul>
(With some CSS from an external stylesheet to apply your presentation).

<?php
$db = &JFactory::getDBO();
$query88 = "SELECT file_url_thumb FROM fs01_virtuemart_medias WHERE virtuemart_media_id=1 LIMIT 0, 30 ";
$result88 = mysql_query( $query88 ) or die( mysql_error() );
while( $row = mysql_fetch_array( $result88 ) ) {
echo '<img src="' . $row[ 'file_url_thumb' ] . '" border="0" style="border: 0; vertical-align: top;" /><br />';
}
?>

use this
<?php
$db = &JFactory::getDBO();
$query88 = "SELECT file_url_thumb FROM fs01_virtuemart_medias WHERE virtuemart_media_id=1 LIMIT 0, 30 ";
$result88 = mysql_query( $query88 ) or die( mysql_error() );
while($row = mysql_fetch_array($result88)){
echo '<img src="'.$row['file_url_thumb'].'" style=" border="0" style="border: 0; vertical-align: top;"/>';
echo '</br>';
}
?>

Related

PHP / MySQL - Query not fetching any data

I have been trying to set up a page that lists prices of items from a table in a database. Here is my code:
<?php
$querylist = mysql_query("SELECT item_name,image,price,added_by FROM values");
while($row = mysql_fetch_array($querylist))
{
echo '<div class="post rareitem" style="margin-right: 15px;float: left;">
<div class="rarename">
<strong>';
// Shows Item Name
echo $row['item_name'];
echo '</strong>
</div>';
// Shows Item Image
echo '<div class="rareimage" style="background-image: url(/app/tpl/skins/Mango/images/values/rares/';
echo $row['image'];
echo ');"></div>';
// Shows Item Price
echo '<div class="rarecontrols">
<div class="coinsbox"></div>
<span>
<b> <b>Credits: </b> </b> ';
echo $row['price'];
echo '</span>';
// Shows Who Added the Item
echo '<div class="addedbox"></div><b><b><span><font color="#c93734"><font color="#c93734">Added By: </font> </font>';
echo $row['added_by'];
echo '</span></b></b>
</div>
<div class="clear"></div>
</div>';
}
?>
There is another chunk of code (shown below) that I have based this off of, and it works perfectly fine. I can't seem to get this to work though. I believe it has something to do with the SQL, the syntax, or something. No matter what I do, it produces absolutely no results, yet the code below results exactly as planned. I know for a fact it is not a connection issue because the below code can be placed on the same exact page as the above one and it works fine, however the above does not.
<?php
$querylist = mysql_query("SELECT id,username,motto,country,look,rank,account_created,role,account_created,online,last_online FROM users WHERE rank='9' ORDER BY ID LIMIT 20");
while($row = mysql_fetch_array($querylist))
{
echo '
<div class="team">';
// Showing Avatar
echo '<div style="float: left; margin-top:-1px;height: 60px; width: 64px; background: url(http://www.habbo.nl/habbo-imaging/avatarimage?figure=';
echo $row['look'];echo "&action=wav&direction=3&head_direction=3&gesture=sml&size=m) no-repeat 0px -10px";
echo "\"/>";
echo "<img alt=\"badge\" src=\"/app/tpl/skins/habbo/images/icons/";
echo $row['online'];echo ".gif\"></div>";
// Flags
echo "<br/><img src=\"/app/tpl/skins/habbo/images/icons/flags/";
echo $row['country'];echo ".png";
echo '" style="float:right;" /> <b><uf>';
echo $row['username'];echo "</u></b>";
// Bans & Ticket Count
$Bans = mysql_query("SELECT * FROM `bans` WHERE `added_by` = '" . $row['username'] . "'");
$BanCount = mysql_num_rows($Bans);
$Tickets = mysql_query("SELECT * FROM `moderation_tickets` WHERE `moderator_id` = '" . $row['id'] . "'");
$TicketCount = mysql_num_rows($Tickets);
//Role
echo "<br/><gb>Role: </b><fi> ";
echo $row['role'];echo "";
echo "</i>";
// Echoing bans & Tickets
echo "<br/><gb>Bans: </b><fi> ";
; echo $BanCount;
echo "</i>";
echo " <gb>Tickets: </b><if>";
; echo $TicketCount;
echo "</i>";
echo "</div>";
}
?>
Thanks in advanced, any assistance will be greatly appreciated!
values is Reserved Words in mysql it should be on backtick
SELECT item_name,image,price,added_by FROM `values`
And stop using mysql it is deprecated. Instead use mysqli or PDO

Increment a value in a sql database when a pdf link is downloaded on a webpage

I'm creating an eBook account on my website: where customers can have a library of ebooks and every time an eBook is downloaded, I need to add 1 to the database for the client to be able to see the number of downloads. I can not figure out the line of code I would need for this and I have done research but nothing can help with this specific query. This is what I have so far, which pulls the information from a sql table and display in html table:
<p>List of Publications</p>
<?php
//connect
$query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<td> <?php if ($numrows > 0){
while($row = mysql_fetch_array($query))
{
?>
<a href="<?php echo $row['Link']; ?>" target="_blank">
<img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></a> <?php
}
} else
echo "Wrong Query";
?>
</tr>
You could use JQuery
ebookDownloads.php
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="javascript/text">
// JavaScript Document
function Downloaded(id) {
var data = {'id':id};
$.post( "downloaded.php", data);
}
</script>
</head>
<p>List of Publications</p>
<?php
//connect $query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<?php
if ($numrows > 0){
while($row = mysql_fetch_array($query)) {
?>
<td><img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></td>
<?php
}
} else {
echo "Wrong Query";
}
?>
</tr>
</table>
downloaded.php
if(isset($_POST['id']) && is_numeric($_POST['id'])) {
$sql = "UPDATE table SET downloaded=downloaded+1 WHERE id = $_POST['id']";
}
EDIT: I have not tested this code. Also the user might not have javascript enabled in which case it wouldn't count the download, so if you need to count the downloads (e.g they pay for so many) you could make sure they have javascript enabled before showing the downloads.
Update: another method using a new tab and PHP to record the download then present it
<p>List of Publications</p>
<?php
//connect $query = "SELECT * FROM AccountTest";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
?>
<table style="width: 80%; border:2px #be9c81 dashed; padding:10px 10px 10px 10px"align="center" >
<tr>
<?php
if ($numrows > 0){
while($row = mysql_fetch_array($query)) {
?>
<td><img style="padding:20px 20px 20px 20px" alt="" src="<?php echo $row['Image']; ?>" /></td>
<?php
}
} else {
echo "Wrong Query";
}
?>
</tr>
</table>
<?php
######## download.php #######################
if(isset($_GET['id']) && is_numeric($_GET['id'])) {
$query = "SELECT * FROM ebooks WHERE id = $_GET['id']";
$query = mysql_query($query);
$numrows = mysql_num_rows($query);
if ($numrows > 0) {
$sql = "UPDATE table SET downloaded=downloaded+1 WHERE id = $_POST['id']"; // update downloaded
while($row = mysql_fetch_array($query)) {
header('Content-Type: application/pdf');
header('Content-disposition: attachment;filename='.$row['name'].'pdf');
readfile($row['link']); // should be an absolute path
}
}
}
?>

Activating certain PHP code before a section of the page is Created

I am working with an HTML webpage connected to a database and using PHP. I have in a div at the top a variable that doesn't get properly set until some PHP later in the document. Is there a way to make the div build after the later PHP, but still be where it should be at the top of the page?
Here's the code that uses the variables
<div id="HeaderRight">
<div id="HeaderRight">
<?php
echo '<a href="../Cart/index.php" class="header">
<img src="../Images/shoppingcart.gif" height="18" width="127" border="0" align="ABSMIDDLE" /> Contains '.$_SESSION["num_items"].' Items</a>'
?>
</div>
Here's the code that sets the variables
<?php
echo '<TABLE WIDTH="100%" BGCOLOR="#FFFFFF" ALIGN = "RIGHT" BORDER = "1">';
echo '<TR BGCOLOR = "818996" HEIGHT = "25"><TD>Product</TD><TD>Availability</TD> <TD>Qty</TD><TD>Price</TD><TD>Total</TD></TR>';
$strTotalPrice = 0;
for ($i=0; $i<$_SESSION['num_products']; $i++) {
$strSQL = "SELECT * FROM products WHERE ProductCode = '".$_SESSION['cart'][$i]['name']."'";
$rsProd = mysql_query($strSQL)
or die($db_name . " : " . $strSQL . " : " . mysql_error());
$rowProd = mysql_fetch_array($rsProd);
$strSQLDetails = "SELECT * FROM pagedetails WHERE Category = '".$rowProd["Category"]."'";
$rsDetails = mysql_query($strSQLDetails)
or die($db_name . " : " . $strSQLDetails . " : " . mysql_error());
$rowDetails = mysql_fetch_array($rsDetails);
// End Of "Retrieve Desired Record Set"
echo ' <TR HEIGHT = "' .$rowProd["ThumbnailHeight"]. '" ><TD style="vertical-align:middle"><img src="../'.$rowProd["Department"].'/Images/'.$rowProd["ProductCode"].'_sm.jpg" align = "left" border="0" height = "'.$rowProd["ThumbnailHeight"].'" width="100" hspace="25" />
<br><a href = "../Products/Products.php?PageID='.$rowDetails["PageID"].'" >'.$rowProd["ProductName"].'</a>
</TD>';
$strAvailability = "";
$strQuantity = 0;
if($rowProd["Stock"] < $_SESSION["cart"][$i]["qty"]){
$strAvailability = "Only " .$rowProd["Stock"]. " in Stock, Quantity Changed";
$strQuantity = $rowProd["Stock"];
$_SESSION["cart"][$i]["qty"] = $rowProd["Stock"];
}
else{
$strAvailability = "In Stock";
$strQuantity = $_SESSION["cart"][$i]["qty"];
}
echo '<TD ALIGN = "center">'.$strAvailability.'</TD>';
echo '<TD ALIGN = "center">
<input type="text" name="txtQuantity" size="2" maxlength="2" value ='.$strQuantity.'>';
echo '<form action="index.php?=?product='.$rowProd["ProductCode"].'&quantity=1" method="post">
<input type="submit" name="submit" value="Update">
</form>
</TD>';
$strPrice = $rowProd["RegPrice"] * $strQuantity;
$strTotalPrice += $strPrice;
echo '<TD ALIGN = "center">$'.$rowProd["RegPrice"].'.00</TD>';
echo '<TD ALIGN = "center">$'.$strPrice.'.00</TD></TR>';
//echo '( <a href=index.php?product=' . $_SESSION['cart'][$i]['name'] . '&quantity=1>+</a> )';
}
As Rocket Hazmat points out, it is a good idea to run all of your PHP code before rendering the HTML. Without rewriting your current code, you can achieve this with some output buffering:
<?
// add this block
function firstDiv() {
ob_start();
?>
<div id="HeaderRight">
... <? echo $_SESSION["num_items"]; ?> ...
</div>
<?
// ... and this block
$_SESSION["firstDiv"] = ob_get_contents();
ob_end_clean();
} // end of firstDiv
?>
and the same with the rest of your page
<?
function restOfYourPage() {
ob_start();
?>
<TABLE> ... <? $_SESSION["num_items"] = 666; ?> ... </TABLE>
<?
$_SESSION["restOfYourPage"] = ob_get_contents();
ob_end_clean();
} // end of restOfYourPage
?>
Then you can run the php code in reverse order, but display it in normal order:
<?
restOfYourPage();
firstDiv();
echo $_SESSION["firstDiv"];
echo $_SESSION["restOfYourPage"];
?>

Alternative to using PHP-GMP to populate two column table

A page is using the PHP GNU Multiple Precision (GMP) to determine how many rows should end up in each column. While the code works, we would like to migrate the application to a new host that does not support the PHP5-GMP module. That said, what might be the best alternative to generating and populating a two column table that may not always have an even number of results? Further the table design is probably not the best so I am open to alternatives.
The code below taken from here gets us close but since it uses mysql_fetch_array, it places the items like:
a b
c d
instead of
a c
b d
Is there a way to have it display in the first example?
Newer code:
<head>
<style>
.container { width: 400px; float: left; }
.container .item { width: 50%; float: left; height: someFixedHeight; }
</style>
</head>
<?php
mysql_select_db("database",$db);
$cat= $_GET["cat"];
echo '<div class="container">';
$q = mysql_query("SELECT * FROM name WHERE Field4 = '$cat'",$db);
while ($res = mysql_fetch_array($q)){
echo '<div class="item">' . $res['Field1'] . '</div>';
}
echo '</div>';
?>
Original code:
<div id="bit-list" align="center" >
<table class="list" width="600" border="0" align="center">
<tr>
<td width="300" height="72" valign="top" border="1">
<div align="left">
<?php
$result = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
$myrow3 = mysql_fetch_row($result);
$num_rows = mysql_num_rows($result);
$d3 = gmp_div_q("$num_rows", "2", GMP_ROUND_PLUSINF);
$i = 1;
$result8 = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
while ($myrow3 = mysql_fetch_row($result8))
{
if ($i <= gmp_strval($d3))
{
echo "<p>";
echo '<a href="detail.php?';
echo 'page=';
echo "$myrow3[2]";
echo '&';
echo 'pnum=';
echo "$myrow3[6]";
echo '">';
echo "$myrow3[1]";
echo "</p>";
$i = $i + 1;
}
else
{
}
}
?>
</div></td>
<td width="300" valign="top" border="1">
<div align="left">
<?php
$result = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
$myrow3 = mysql_fetch_row($result);
$num_rows = mysql_num_rows($result);
$d4 = gmp_div_q("$num_rows", "2", GMP_ROUND_MINUSINF);
$d3 = gmp_div_q("$num_rows", "2", GMP_ROUND_PLUSINF);
$j = 1;
$result18 = mysql_query("SELECT * FROM name WHERE field4 = '$cat' ",$db);
while ($myrow3 = mysql_fetch_row($result18))
{
if ($j <= gmp_strval($d3))
{
$j=$j+1;
}
else
{
echo "<p>";
echo '<a href="detail.php?';
echo 'page=';
echo "$myrow3[2]";
echo '&';
echo 'pnum=';
echo "$myrow3[6]";
echo '">';
echo "$myrow3[1]";
echo "</p>";
$j = $j + 1;
}
}
?>
</div>
</td>
</tr>
</table>
</div>
If i read well, you use gmp_div_q for rounding only? Then, you should check round().
The only case, that round() cannot cover is GMP_ROUND_ZERO, but you don't use that (and you could cover it with a simple if).

PHP Value of Variable is undefined

I am getting " Notice: Undefined index:k" in the PHP code. K is the name of the text field and I am using $_GET[] method to get the value of k. In the below mentioned example I am trying to keep the value available even after the form is submitted. This Code runs fine for the first time but the second time it gives the above error.
<form name="keywordquery" method="get" action="keywordsearch.php">
<fieldset class="fieldsetclass"><legend class="legendclass">Search by
Keywords</legend>
<div id="searchbox">
<input type="text" name="k" value="<?php if(isset($_GET['k'])){echo
htmlentities($_GET['k']);} ?>" style="border: 1px, thin; width:92%; "/> <input
type="image" style="margin-bottom: 0; margin-top: 2px;" src="search.png"
value="submit" />
</div>
</fieldset>
</form>
</div>
<table id="dataTable" cellpadding="0" cellspacing="0" border="1" style="border-
radius:20px; box-shadow: 9px 5px 8px #7E9044;">
<tbody>
<?php
// PAGINATION Code. check if the starting row variable was passed in the
URL or not
if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
//we give the value of the starting row to 0 because nothing was found in URL
$startrow = 0;
//otherwise we take the value from the URL
} else {
$startrow = (int)$_GET['startrow'];
}
$k1 = $_GET['k'];
$term = explode(" ", $k1);
$query = "SELECT * FROM data ";
foreach ($term as $each) {
$i++;
if($i==1)
{
$query .= "WHERE keywords LIKE '%$each%' ";
}
else {
$query .= " OR WHERE keywords LIKE '%$each%' ";
}
}
$query .= "LIMIT $startrow, 1";
$connection = mysql_connect("xxxx", "xxxxx","");
if(!$connection)
echo "No database connected";
$dbase = mysql_select_db("xxxxxxxx", $connection);
if(!$dbase)
echo "No datatable connected";
$ourquery1 = mysql_query ($query);
if(!$ourquery1)
echo "No query found";
$row1 = mysql_num_rows ($ourquery1);
if ($row1 > 0)
{
while($result = mysql_fetch_assoc($ourquery1))
{
echo "<tr>";
$title = $result['title'];
$link = $result['link'];
$region = $result['region'];
$sector = $result['sector'];
$theme = $result['theme'];
echo "<td> <a href=$link><h3>$title<h3></a>";
echo "<h4>Sector: $sector
Theme: $theme
<br> Region: $region </td>";
}
}
echo "</tbody>";
echo 'Next';
echo 'Prev';
Replace the line:
$k1 = $_GET['k'];
with something like:
$k1 = isset($_GET['k'])? $_GET['k'] : $default_k_value;
You don't show the complete form so it is tough to tell what is wrong but here is a hint. Swap $_GET with $_REQUEST.
Example:
<?php if(isset($_REQUEST['k'])){echo
htmlentities($_REQUEST['k']);} ?>
If the form is using a POST method, the value would be in $_POST. If the form uses a GET method, the value would be in $_GET. However $_REQUEST will contain the form fields whether the form used POST or GET.

Categories