I have a function wich prints a number of credits:
<?php
function selectCredits()
{
include 'sqlvars.php';
$con = mysql_connect("localhost","bbbb","bbbb");
if (!$con){
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bbbb", $con);
$result = mysql_query($selectCredits);
while($row = mysql_fetch_array($result)){
echo $row['credits'];
}
}
?>
When I'm calling it from a php file by using:
<?php
include 'sql.php';
selectCredits();
?>
I can see the output, but when I'm inside of a HTML document I can't get the result using this code:
<center><b>Your credits: </b></center> <?php include 'sql.php'; selectCredits(); ?>
The output is always: Your credits: without the query result.
For sure I'm missing somthing really small. I'm not a php guy, but I'm willing to learn it, already lost an hour without any success.
If you really want html files to execute php code, try to add this line to htaccess:
AddType application/x-httpd-php .html
check your query statement '$selectCredits'. check table name and attribute name-'credits'. The $row['credits'] is not getting its value.
You have a problem in your code. You have written
while($row = mysql_fetch_array($result)){
echo $row['credits'];
}
You have used mysql_fetch_array and you have echoed $row with attribute value directly. If you are using mysql_fetch_array, then use $row[0], $row[1] or $row[2] etc. All the attributes come as an array. Thats why you should treat it as array index value. So, if "credit" attribute is at the 5th column in your database, then you should echo it as
echo $row['4'];
If you use mysql_fetch_assoc() instead of mysql_fetch_array, then you can write the attribute name directly.
E.g. then you can write
echo $row['credits'];
I hope, this will solve the issue. Please ask me if you have any further questions.
You should use a JavaScript tag to implement the PHP file like this:
<script type="text/javascript" src="credits.php"></script>
credits.php would have to output:
echo "document.write('" . $row['credits'] . "');";
Simple :)
Related
I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>
So I'm basically calling and returning an entire row from a mysql table using a while loop (which is working), but I'm trying to use the data that I call inside an html link, but I can't seem to get it to work.
Ideally, eventually it will just be a list of links with each person's individual name. I can return the list fine, but I can't seem to return the list with a link.
Here is my code that I feel should be working :(
<?php
require 'db/connect.php';
$result = $con->query('SELECT distinct name FROM mytable');
while($rows = $result->fetch_assoc())
{
echo ''$rows['name']'' , "</br>";
}
?>
Any help would be greatly appreciated!
Issue might be with your string concatenation. Try following code block
echo ''.$rows['name'].'';
echo ''. $rows['name']. '' , "</br>";
You just need to use . to concatenate strings together.
try this
echo ''.$rows['name'].'' , "</br>";
Should work just fine. Basically it's '.$row['name'].'
when concatenating strings with variables you have to use dot(.) like echo "string".$var; it will be invalid to write echo "string"$var; in your example you have ignored this point.
What I am trying to do is get an echo of the following php call and subtract 14.1% from the displayed number.
The code:
<?php echo $program->current_amount(); ?>
Can I add arithmetic functions to this in order to display the 14.1% deduction?
I think you're looking for a basic math operation in your output that has no effect on a database or anything else, correct?
If so, do something like the following:
<?php
// Set values
$current_amount = 100;
$pcnt_off = 14.1;
// Do the math
$out = $current_amount - ($pcnt_off/100) * $current_amount;
// Output
echo $out . " is " . $pcnt_off . "% off of " . $current_amount;
?>
http://codepad.org/RqF8cuvN
More specifically to your case:
<?php echo $program->current_amount() - 0.141 * $program->current_amount(); ?>
You can perform expressions inside an echo statement, yes; just wrap it in a (), so:
<?php echo ($program->current_amount() - .141); ?>
It may not even be necessary to use (). Incidentally, if your environment supports short tags, you can simply do:
<?= $program->current_amount() - .141 ?>
Keep in mind, though, that that code won't actually remove 14.1% from your number--you would want to multiply by .859.
I am storing in a mySQL table the HTML/PHP content of individual slides to be displayed on a single page.
Here is an example of HTML/PHP code stored in the mySQL table:
<p>Welcome <?php echo $userData['fname']; ?>!</p>
<p>You made it to the first slide!</p>
I retrieve the content of the slides in PHP with the following code:
<?php
$fetchedPageSlideData = mysql_query("SELECT * FROM pageSlides WHERE pageID = $pageID ORDER BY 'order' DESC") or die(mysql_error());
while ($pageSlideData = mysql_fetch_array($fetchedPageSlideData)) {
$pageSlideContent = $pageSlideData['content']; ?>
<div><?php echo $pageSlideContent; ?></div>
<?php }
?>
All of the HTML of the content displays correctly, but the PHP is inserted as follows:
<!--?php echo $userData['fname']; ?-->
So the PHP is commented out and doesn't display.
How can I retrieve the HTML/PHP code and have the PHP not commented out?
It might be a better idea to use placeholder strings in the DB data. Executing arbitrary php code from a DB can be dangerous. PHP is Evil
Look into PHP function eval(): http://php.net/manual/en/function.eval.php
Dropping in and out of the PHP interpreter makes your code rather difficult to read. Consider:
<?php
$f = mysql_query(
"SELECT *
FROM pageSlides
WHERE pageID = $pageID
ORDER BY 'order' DESC"
) or die(mysql_error());
while ($d = mysql_fetch_array($f)) {
print "<div>" . $d['content'] . "</div>\n";
}
Regardless there is no implicit nor explicit mechanism here which would inject the comment tags you've presented. However it may be the browser trying to make sense of the unescaped html code and <?php ... ?> tags.
Try:
print "<div>" . htmlentities($d['content']) . "</div>\n";
As a side note, you might consider using
print "<div>" . highlight_string($d['content']) . "</div>\n";
Or do you mean that you actually want to run the code stored in the database - if so, you're asking for a world of pain. Eval is not evil - but you really must know what you're doing to avoid getting bitten by it.
I want to:
Read in text line from "textfile.txt".
'echo' that line to the page in a <div> element.
Read in a text line from "namefile.txt".
Make this line become some sort of pop-up-text for that <div> element.
My script:
<? PHP
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");
while(!feof($fhtext))
{
echo "<div title="HERE IS WHERE I AM STUCK">".fgets($fhtext)."<div/><br />";
}
Could I perhaps go:
echo "<div title="<? fgets($fhname) ?>".fgets($fhtext)."<div/><br />";
?
<?php
$fhtext = fopen("textfile.txt","a+") or exit("Error 1");
$fhname = fopen("namefile.txt","a+") or exit("Error 2");
while(!feof($fhtext) && !feof($fhname))
{
echo "<div title=\"", fgets($fhname), "\">", fgets($fhtext), "<div/><br />";
}
?>
I haven't used PHP in a long time, but this should work:
echo "<div title='" . fgets($fhname) ."'>" .fgets($fhtext). "<div/><br />";
Regarding:
Make this line become some sort of pop-up-text for that '' element.
If you mean 'popup' text, as in tooltips of the type you get when you hover over links/images, this is only available on some elements when their title attribute has been set, not DIVs.
As such you can either change the DIV to a A (link) element. Or use Javascript to detect a hover over the DIV and display a popup.
If you are sure both files have the same number of lines you could use the „file“-function of PHP. This will read the file into an array and you can loop over it with a for-loop:
<?php
$file1 = file('file1');
$file2 = file('file2');
for ($i = 0, $max = count($file1); $i < $max; $i++) {
echo $file1[$i].' '.$file2[$i];
}
Before you dump your fgets() data to the browser, you really ought to HTML encode it first. That will prevent accidental (or not so accidental) problems caused by HTML fragments that might be in your text files, or if the file name can be entered by the user (either as part of the URL or as part of a form).
As a rule of thumb, always HTML encode anything coming from a data source you don't control before spitting it out to the browser. That includes form fields, etc.