I have install DirectPHP and it works fine, I can actually print out the current time with PHP, so I know it works.
My problem lies in using a custom PHP to call out variables and print out in all my articles.
First: My custom php inside /media/custom.php
<?php
// Rental Prices Equipment
$miniraeClassic_d = "$35.00";
$miniraeClassic_m = "$120.00";
$miniraeClassic_y = "$400.00";
$message = "Hello World";
?>
Second: Here is where I included the PHP file I want to retrieve information from. This was included in the index.php of my template.
// Preparing template parameters
JSNTplTemplateHelper::prepare();
// Get template utilities
$jsnutils = JSNTplUtils::getInstance();
?>
<?php
include '/media/custom.php';
?>
<!DOCTYPE html>
<!-- <?php echo $this->template . ' ' . JSNTplHelper::getTemplateVersion($this->template); ?> -->
<html lang="<?php echo $this->language ?>" dir="<?php echo $this->direction; ?>">
<head>
Third: In my article I added two codes, one trying to retrieve $message from my custom.php, and another to test if PHP actually works in my article (which prints out the time).
<?php
echo $message;
?>
Now is: <?php echo date('Y-m-d H:i:s');?>
Fourth: here is the result, in my article, it does not print out $message, but it does print out the date and time.
It did not print out $message
in my article, the only thing it printed out was
Now is: 2014-05-28 22:21:50
I want to call out multiple variable around my article i.e.
<table border="0" width="100%">
<tr>
<td width="32%" align="center" class="table-title"><b>Daily</b></td>
<td width="34%" align="center" class="table-title"><b>Weekly</b></td>
<td width="34%" align="center" class="table-title"><b>Monthly</b></td>
</tr>
<tr>
<td align="center" class="table-content"><?= $dayrental?></td>
<td align="center" class="table-content"><?= $weekrental?></td>
<td align="center" class="table-content"><?= $monthrental?></td>
</tr>
Try using the following to import your custom php file:
<?php
include(JUri::root() . 'templates/' . $this->template . '/media/custom.php');
?>
JUri::root() defined the root of your Joomla installation which you have not defined in your code.
Related
I need to pass value with href inside fetch array echo...
php code
$user = $get['username'];
$resource=mysqli_query($con,$sql);
echo "<font color=\"#000000\">
<h2 align=\"center\"></h2>
<table align=\"center\" border=\"1\" width=\"50%\">
<tr>
<td><b>GROUP NAME</b></td> <td><b>TASK TITLE 1</b></td> <td><b>TASK TITLE 2</b></td> <td><b>CREATED BY</b></td> <td><b>ASSIGNED TO</b></td> <td><b>DUE DATE</b></td> <td><b>PRIORITY</b></td> <td><b>CHANGE</b></td></tr> ";
while($result=mysqli_fetch_array($resource))
{
echo "<tr><td>".$result[0]."</td> <td>".$result[1]."</td> <td>".$result[2]."</td> <td>".$result[3]."</td> <td>".$result[4]."</td> <td>".$result[5]."</td> <td>".$result[6]."</td> <td> "<a href="changetask1.php?username='.$user.'">"</td></tr>";
} echo "</table></font>";
I need to pass username to next page with href value.
You are all messed up with quotes...
First... Learn to indent your code to make it readable.
Then, avoid echoing simple HTML if not necessary:
See your code after my «formatting»:
Try it, i've done a couple corrections...
<?php
$user=$get['username'];
$resource=mysqli_query($con,$sql);
?>
<!-- This is only HTML -->
<font color="#000000">
<h2 align="center"></h2>
<table align="center" border="1" width="50%">
<tr>
<td><b>GROUP NAME</b></td>
<td><b>TASK TITLE 1</b></td>
<td><b>TASK TITLE 2</b></td>
<td><b>CREATED BY</b></td>
<td><b>ASSIGNED TO</b></td>
<td><b>DUE DATE</b></td>
<td><b>PRIORITY</b></td>
<td><b>CHANGE</b></td>
</tr>
<?php
// This is a PHP block until the next ?>
while($result=mysqli_fetch_array($resource)){
echo "<tr>
<td>".$result[0]."</td>
<td>".$result[1]."</td>
<td>".$result[2]."</td>
<td>".$result[3]."</td>
<td>".$result[4]."</td>
<td>".$result[5]."</td>
<td>".$result[6]."</td>
<td><a href='changetask1.php?username=".$user."'></td>
</tr>";
}
?>
</table>
</font>
To avoid these problems of double and single quote you can write your code like following.
<td><a href="cheangetask1.php?username=<?php echo $user;?>"></td>
Now on the changetask1.php you can get username like this.
<?php echo $_REQUEST['username'];?>
I"m having trouble with my code hopefully someone can help.
I'm trying to call information using "php echo" to display information in table form and it works except for the links which doesn't recognize the $id. If I don't put it in the table form it works fine but it is not aesthetically appealing.
Any suggestions would be greatly appreciated!
<?php
session_start();
if(!isset($_SESSION['name'])){
header("location: ../index.php");
exit();
}
// Script Error Reporting
error_reporting(E_ALL);
ini_set('display_errors', '1');
include_once("../scripts/connect.php");
// Delete Item Question to Admin, and Delete Product if they choose
if (isset($_GET['deleteid'])) {
echo 'Do you really want to delete messages with ID of ' . $_GET['deleteid'] .'? Yes | No';
exit();
}
if (isset($_GET['yesdelete'])) {
// delete from database
$id_to_delete = $_GET['yesdelete'];
$sql = mysql_query("DELETE FROM `mystore`.`messages` WHERE `messages`.`id` = '$id_to_delete' LIMIT 1") or die (mysql_error());
}
$messages = "";
$sql = mysql_query("SELECT * FROM messages ORDER BY msg_date DESC LIMIT 20");
$count = mysql_num_rows($sql);
if($count > 0){
while($row = mysql_fetch_array($sql)){
echo '<tr>';
echo '<td>'.$row['msg_name'].'</td>';
echo '<td>'.$row['msg_email'].'</td>';
echo '<td>'.$row['msg_subject'].'</td>';
echo '<td>'.$row['msg_date'].'</td>';
echo '<td>Reply</td>';
echo '<td>Delete</td>';
echo '</tr>';
}
}else{
$messages = "<b>There are no messages in the database at this moment</b>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Messages</title>
<meta charset="utf-8" />
<link rel="stylesheet" href="style/forms.css" media="screen">
<link rel="stylesheet" href="style/main.css" media="screen">
</head>
<body>
<div id="main_wrapper">
<?php include_once("templates/tmp_header.php"); ?>
<?php include_once("templates/tmp_nav.php"); ?>
<section id="main_content">
<h2 class="page_title">Messages</h2>
<br/>
<table width="730" cellspacing="0" cellpadding="3" border="1">
<tr>
<td align="center" width="100">From</td>
<td align="center" width="300">Email</td>
<td align="center" width="300">Subject</td>
<td align="center" width="100">Date</td>
<td align="center" width="100">Actions</td
></tr>
<?php echo $messages; ?>
</table>
</section>
<?php include_once("templates/tmp_aside.php"); ?>
<?php include_once("templates/tmp_footer.php"); ?>
</div>
Please change
echo '<td>Delete</td>';
to
echo "<td><a href='admin_messages.php?deleteid=$id'>Delete</a></td>";
when trying to print out a variable the main string has to be wrapped in double quotes.
If you want to interpolate variables in PHP, you need to use double quotes. echo '$id' will literally print $id, whereas echo "$id" will print the value of the variable. However, I would recommend an alternative approach. Don't use PHP where it isn't needed. There's no need to use echo so much.
I would change the contents of your loop to this:
?>
<tr>
<td><?=$row['msg_name']?></td>
<td><?=$row['msg_email']?></td>
<td><?=$row['msg_subject']?></td>
<td><?=$row['msg_date']?></td>
<td>Reply</td>
<td>Delete</td>
</tr>
<?php
The <?=$id?> is shorthand for <?php echo $id?> and is supported by default in PHP versions >=5.4.0. You can also use it in previous versions if you enable short_open_tags.
As stated in the comments, you should really be using mysqli functions, as mysql functions are deprecated.
As you may know, Joomla components enable you to override their output by copying their template files into your site template. Joomla components generally use helper files which cannot be overridden.
I have a helper.php file that includes the string:
$specific_fields_text = '<tr><td class="key">'.$specific_field_title.': </td><td class="kr_sidecol_subaddress">'.$specific_fields[$i]->text.' '.$specific_fields[$i]->description.'</td></tr>';
In my template override is the code:
<table border="0" cellpadding="2" cellspacing="0">
<?php echo koparentHTML::getHTMLSpecificFields($this->specific_fields); ?>
</table>
The output is as follows:
<table border="0" cellpadding="2" cellspacing="0">
<tr>
<td class="key">title</td>
<td class="kr_sidecol_subaddress">value</td>
</tr>
<tr>
<td class="key">title</td>
<td class="kr_sidecol_subaddress">value</td>
</tr>
//.....etc......//
</table>
Basically I want to get rid of the table and turn it into a definition list but I cannot modify the helper.php file. I am thinking that the answer is to do with str_replace
I have tried using:
<dl>
<?php
$spec_fields = koparentHTML::getHTMLSpecificFields($this->specific_fields);
$spec_fields_dl = str_replace("<tr><td class='key'>'.$specific_field_title.': </td><td class='kr_sidecol_subaddress'>'.$specific_fields[$i]->text.' '.$specific_fields[$i]->description.'</td></tr>'", "<dt class='key'>'.$specific_field_title.': </dt><dd class='kr_sidecol_subaddress'>'.$specific_fields[$i]->text.' '.$specific_fields[$i]->description.'</dd>'", $spec_fields);
echo $spec_fields_dl;
?>
</dl>
This returns all of the text but with no html tags (no tr, td, dt, etc).
You can easily parse table data with PHP, like in this example:
$doc = new DOMDocument();
$doc->loadHTML(koparentHTML::getHTMLSpecificFields($this->specific_fields));
$rows = $doc->getElementsByTagName('tr');
$data = array();
for ($i = 0; $i < $rows->length; $i++) {
$cols = $rows->item($i)->getElementsbyTagName("td");
$data[$cols->item(0)->nodeValue] = $data[$cols->item(1)->nodeValue];
}
var_dump $data;
This should convert your table into assoc array ('title' => 'value').
I hope it helps.
I have figured this out. For some reason the PHP bits such as '.$specific_field_title.' where stopping the str_replace from working. To get around this I just searched for the HTML elements and put them in an array like so:
echo str_replace(array('<tr><td class="key">', '</td><td class="kr_sidecol_subaddress">', '</td></tr>'),
array('<dt class="key">', '</dt><dd class="kr_sidecol_subaddress">', '</dd>'),
koparentHTML::getHTMLSpecificFields($this->specific_fields));
And now this works perfectly. Thank you to everyone who contributed.
I have a HTML content in a variable like this.
$message ="<div>
<table align='center'>
<tr><td><h1>Reporte de Tipo de Documentos</h1></td></tr>
<tr><td><h2>Farmaceutica MDC</h2></td></tr>
</table>
</div>
<div>
<table style='width:960px; margin:0 auto;'>
<tr colspan='7'>
<td><?php echo 'Fecha: '".$time = date('d/m/Y h:i:s A')."; ?></td>
</tr>
<tr bgcolor='#CCCCCC' height='30'>
<td><b>Sr. No.</b></td>
<td><b>Tipo Documento</b></td>
<td><b>Cant. Pasos</b></td>
<td><b>Costo</b></td>
<td><b>Precio</b></td>
<td><b>Balance</b></td>
<td><b>Notifica Cliente</b></td>
</tr>
</table>";
I want to print a printout page of this HTML Content.
Like for web page we use
<script type="text/javascript">
function printpage()
{
window.print();
}
</script>.
So what i use to print the above HTML content?
How about embedding the contents of this page in HTML and adding a script to print it out automatically on page load?
<?php
$message ="<div>
<table align='center'>
<tr><td><h1>Reporte de Tipo de Documentos</h1></td></tr>
<tr><td><h2>Farmaceutica MDC</h2></td></tr>
</table>
</div>
<div>
<table style='width:960px; margin:0 auto;'>
<tr colspan='7'>
<td><?php echo 'Fecha: '".$time = date('d/m/Y h:i:s A')."; ?></td>
</tr>
<tr bgcolor='#CCCCCC' height='30'>
<td><b>Sr. No.</b></td>
<td><b>Tipo Documento</b></td>
<td><b>Cant. Pasos</b></td>
<td><b>Costo</b></td>
<td><b>Precio</b></td>
<td><b>Balance</b></td>
<td><b>Notifica Cliente</b></td>
</tr>
</table>";
echo "<html><head></head><body>" . $message . "<script type='application/javascript'>window.onload=function(){window.print()}</script></body></html>";
?>
To literally print the HTML code, you can sanitize the HTML like:
echo "<html><head></head><body>" . htmlspecialchars($message, ENT_QUOTES) . "<script type='application/javascript'>window.onload=function(){window.print()}</script></body></html>";
?>
More information about this:
What's the best method for sanitizing user input with PHP?
Sanitizing HTML input
PHP HTML sanitizer
http://se2.php.net/manual/en/function.htmlspecialchars.php
<input type="submit" value="Print" onclick="printIframe(report);"/>
<script>
function printIframe(objFrame) {
objFrame.focus();
objFrame.print();
bjFrame.save();
}
</script>
<iframe name="report" id="report" src="report.php"></iframe>
try calling the form inside an iframe then a link or button that will run the js to print the html/php inside the iframe..
<?php if ($this->checkPosition('image')) : ?>
<?php
echo "<table class=\"remove-margin-t\" cellpadding=\"0\" cellspacing=\"0\" width=\"97%\" align=\"center\" border=\"0\" style=\"max-width:625px; border:1px solid;\" background=\"..\images";
?>
<?php
echo $this->renderPosition('image')
<?php
echo ".png\">";
?>
<?php endif; ?>
I am trying to figure out how to call the image properly. echo for image is called and has a specific name like 'pink','blue','green' etc. However, it depends on the position part...
This is what it is supposed to look like in html.
<table cellpadding="0" cellspacing="0" width="97%" align="center" border="0" style="max-width:625px; border:1px solid #CCC" background="http://localhost/images/[insert color name here].png" >
Here is the original php
<?php if ($this->checkPosition('color')) : ?>
<?php echo $this->renderPosition('color'); ?>
<?php endif; ?>
Any help would be appreciated. I am sure it must be a '\' or '"' issue.
Best,
Steven
To Jared:
Do you mean like this?
<?php if ($this->checkPosition('image')) : ?>
<?php
echo "<table class=\"remove-margin-t\" cellpadding=\"0\" cellspacing=\"0\" width=\"97%\" align=\"center\" border=\"0\" style=\"max-width:625px; border:1px solid;\" background=\"../images/";
echo $this->renderPosition('image')
echo ".png\">";
?>
<?php endif; ?>
You don't need to open/close PHP tags on every line of PHP code. Your code may be rewritten this way:
<?php
if ($this->checkPosition('image')) {
echo '<table class="remove-margin-t" cellpadding="0" cellspacing="0" width="97%" align="center" border="0" style="max-width:625px; border:1px solid;" background="../images"' . $this->renderPosition('image') . '.png">';
}
?>
I replaced some double quotes with single quotes to avoid using backslashes everywhere.
I concatenated your text so that only one echo is used.
And I fixed a possible mistake at the end of the first echo: I replaced the blackslash by a slash, since directory separators in URLs are slashes.
I don't know what object '$this' is, nor do I know what the method checkPosition does
Also, what output 'renderPosition('color') produces.
either way, this code
<?php if ($this->checkPosition('color')) : ?>
<?php echo $this->renderPosition('color'); ?>
<?php endif; ?>
is improper, and should be written as:
<?php
if ($this->checkPosition('color')) {
echo $this->renderPosition('color');
}
?>
With that said, the server tags "?php" and "?" represent the beginning and end of server code. So outside of those tags is standard html markup, generally.
So, you can use html markup outside of server code as so,
<?php if ($this->checkPosition('color')) { ?>
<div style="width:97%;text-align:center;max-width:625px;border:1px solid #CCC;background-image:url('<?php echo "http://localhost/images/" . $this->renderPosition('color') . ".png"; ?>');display:inline-block;position:relative;">
</div>
<?php } ?>
I turned your table into a div, and used CSSstyleAttributes, instead of depreciated html attributes.
Also, I am also assuming the output of renderPosition is a filename, without the file extension.
EDIT:
localhost refers to your own computer.
You may want to use:
echo "//" . $_SERVER['SERVER_NAME'] . "/images/" . $this->renderPosition('color') . ".png";
in place of
echo "http://localhost/images/" . $this-renderPosition('color') . ".png";