html in php to call image - php

<?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";

Related

Inserting php if statement within php code?

I have a php script that is basically querying my database and providing details about each store, and including an image. The code pulls in the image fine, but currently if no image exists in the database, I have spacer image being inserted instead. Instead of using a spacer image though, is there a way for me to use a PHP 'if' statement inside of the php code to not render an image if the database has no image listed? Here is the basic query code:
<?php
if(strlen($query) >= $min_length){$query = htmlspecialchars($query);
$query = mysql_real_escape_string($query);
$raw_results = mysql_query("SELECT * FROM stores WHERE `TRAVEL` = '1' AND `STATE` = '$query'") or die(mysql_error());
if(mysql_num_rows($raw_results) > 0){
while($results = mysql_fetch_array($raw_results)){
echo "<table width='150' border='3'>
<tbody><tr>
<td>".$results['NAME']."<br>
<img align='left' src='images/images/".$results['IMAGE']."'>
</td>
</tr></tbody>
</table>";
}
} else{ echo "No results were found";
}
} else{ echo " ".$min_length; }
?>
For the php if statement, I was thinking something along these lines:
<?php if ($results['IMAGE'] != '') { ?>
<img src='images/icons/".$results['IMAGE']."' height="100" width="auto">
<?php }?>
You're pretty close. Just use one more <?php ... ?> block in the img
<?php if ($results['IMAGE'] != '') { ?>
<img src='images/icons/<?php echo $results['IMAGE'] ?>' height="100" width="auto">
<?php }?>
You can use the alternative control flow syntax too. It reads a little nicer than spanning the { and } across multiple <?php tags
<?php if ($results['IMAGE'] != ''): ?>
<img src='images/icons/<?php echo $results['IMAGE'] ?>' height="100" width="auto">
<?php endif ?>
Shameless plug:
Generating HTML with PHP gets really ugly. At least in my opinion. To make the job a little nicer/easier, I made htmlgen, mirrored on packagist.
Simply like this :
if(isset($results['IMAGE'])){
echo "<table width='150' border='3'>
<tbody><tr>
<td>".$results['NAME']."<br>
<img align='left' src='images/images/".$results['IMAGE']."'>
</td>";
}

The word "(Array)" is printing in my table instead of the actual objects array

All the code is within the same index file and same class. I can't seem to figure out why its not calling. Both objects themselves work in a call 2-3 lines right above, outside this table.... I tried calling them inside the table to no avail. They just pop up the word '(Array)' in both boxes. Is their a syntactical thing i am missing? On my website I print the table, with the missing functions inside table, and the two tables that are supposed to be inside the table printed right above and below the table. For reference, my website for the school project: http://www.wallofkron.x10host.com
<?php
$companyobject = new BurgerJoint(); //object created
$companyobject->getLeftNavBar($companyobject->navbar_array); //call to leftnav bar works
$companyobject->displayProduct($burgerarray); // burger table also works outside of table
print "<TABLE style='width:100%'height=200 BORDER='1'>
<TR><TD style='width:15%'>$companyobject->getLeftNavBar($companyobject->navbar_array)
</TD>
<TD>$companyobject->displayProduct($burgerarray)
</TD></TR>
</TABLE>";
function displayProduct($array){
print "<TABLE BORDER = '1'>";
foreach ($array as $oneitem) {
print "<TR><TD>$oneitem</TD></TR>";
} print "</TABLE>";
}
function getLeftNavBar($array){
print "<TABLE BORDER = '1'>";
foreach($array as $key => $value) {
print "<TR><TD><A HREF=$value>$key</A></TD></TR>";
}
print "</TABLE>";
}
?>
i think i noticed the problem:
<?php
print "<TABLE style='width:100%'height=200 BORDER='1'>
<TR><TD style='width:15%'>$companyobject->getLeftNavBar($companyobject->navbar_array)
</TD>
<TD>$companyobject->displayProduct($companyobject->burger_array)
</TD></TR>
</TABLE>";
?>
i think that the problem was that you were not reffering to the actual burgerarray inside the object companyobject.
as told in the comment it would look something like this:
<!DOCTYPE html>
<html>
<head>
<title>Bob's Burgers</title>
</head>
<body>
<header style="background:orange; width:100%;">
<div style="text-align:center;">
<?php foreach($companyobject->navbar_array as $nav){
echo $nav
}?>
</header>
<main style="width:100%;">
<table style="height:200px; border:1px solid black;">
<?php foreach($companyobject->burger_array as $burger):?>
<tr><td><?php echo $burger; ?></td></tr>
<? endforeach; ?>
</table>
</main>
</html>

fetch tags inside php

I am trying to get some div tags inside my php. The original code that is working looks like this:
echo "Date: ".$row{'date'}."<br>"."Day: ".$row{'day'}."<br>"."From Time: ".$row{'fromtime'}."<br>"."To Time: ".$row{'totime'}; //display the results
When I run this code, the data is gonna be below each other. I would like that each output from the database, is coming out in a row and in some div tags, so I can control it. I have tried to make it, but I am really in doubt how the syntax are?
<?php
echo "<div class="column1">""Date: ".$row{'date'}"</div>";
echo "<div class="column2">""Day: ".$row{'day'}"</div>";
echo "<div class="column3">""From Time: ".$row{'fromtime'}"</div>";
echo "<div class="column4">""To Time": ".$row{'totime'}"</div>";
?>
This dosent work. Does anybody know how I can solve this?
New code:
Thanks a lot for both your answers. I am actually thinking of putting in a table instead. When I print out the div, it looks like this:
http://postimg.org/image/jq60jujv1/
So I guess it would be something with
//fetch the data from the database
Print"<h3>Return from database:</h3>"."<br>";
print '<table border="1">';
print '<tr>';
print '<td>date</td>';
print '<td>day</td>';
print '<td>fromtime</td>';
print '<td>totime</td>';
print '</tr>'."<br>";
print '</table>'."<br>";
while ($row = mysql_fetch_array($result)) {
print "<tr>"."<td>"."Date: ".$row{'date'}."</td>"."</tr>";
print "<tr>"."<td>"."Day: ".$row{'day'}."</td>"."</tr>";
print "<td>"."From Time: ".$row{'fromtime'}."</td>";
print "<td>"."To Time: ".$row{'totime'}."</td>"."<br>";
}
or?
Best Regards
Mads
try this instead of yours,
<?php
echo "<div class='column1'>"."Date: ".$row{'date'}."</div>";
echo "<div class='column2'>"."Day: ".$row{'day'}."</div>";
echo "<div class='column3'>"."From Time: ".$row{'fromtime'}."</div>";
echo "<div class='column4'>"."To Time: ".$row{'totime'}."</div>";
?>
Because of the wrapping quotation marks you use for echo ".." you can not use the same quotation marks inside that string:
echo "<div class="column1">""Date: ".$row{'date'}"</div>";
^--- string ended
You can solve this in several ways, using single quotation marks inside is already suggested, but you can also escape the ones you are currently using:
echo "<div class=\"column1\">Date: ".$row{'date'}."</div>";
^ ^
You can avoid all these problems by not using echo for output of all markup, but rather start/end php tags the place you actually want to print
variables or do some logic:
<div class="column1">Date: <?php echo $row{'date'}; ?></div>
Output as table
A HTML table use the <table> and </table> tags,
<tr> is a table row, <th> for table
headers and <td> for regular table cells.
<?php
/* the code block where you connect to db etc.. */
?>
<table>
<!-- headers -->
<tr>
<th>Date</th>
<th>Day</th>
<th>From</th>
<th>To</th>
</tr>
<!-- Now a row for each new set of data, here you probably need to
loop through some data set you retrieve from the database -->
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row{'date'};?></td>
<td><?php echo $row{'day'};?></td>
<td><?php echo $row{'fromtime'};?></td>
<td><?php echo $row{'totime'};?></td>
</tr>
<?php endwhile; ?>
</table>
Try this
<?php
echo "<div class='column1'>Date: ".$row{'date'}."</div>
<div class='column2'>Day: ".$row{'day'}."</div>
<div class='column3'>From Time: ".$row{'fromtime'}."</div>
<div class='column4'>To Time: ".$row{'totime'}."</div>";
?>
Edit:
If you want output as table
<table>
<tr>
<th>Date</th>
<th>Day</th>
<th>From Time</th>
<th>To Time</th>
</tr>
<?php
foreach($result as $row){
echo "<tr><td>".$row{'date'}."</td>
<td>".$row{'day'}."</td>
<td>".$row{'fromtime'}."</td>
<td>".$row{'totime'}."</td></tr>";
}
?>
</table>

Joomla PHP include call variables in article from custom PHP

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.

Inserting different values in php echo

I have a login script with a Twitter-like posting script and I tried to insert the user name into the posts but that didn't work. They are both using the same database but I can't figure out why. Here is the php.
<?php
session_start();
require_once 'database.php';
if (isset($_SESSION['user'])){
echo "Welcome ".$_SESSION['user'];
?>
<?php
$posts = show_posts($_SESSION['userid']);
if (count($posts)){
?>
Now the posts part
<table class="imagetable">
<table align="center" border='0' cellspacing='0' cellpadding='5' width='300'>
<td background="cell-blue.jpg">
<?php
foreach ($posts as $key => $list){
echo "<tr valign='middle'>\n";
echo "<td>".".$_SESSION['user']" . "<p>'s BFFL is</p>".$list['body'] ."<br/>\n ";
echo "<small>".$list['stamp'] ."<hr>"."</small></td>\n";
echo "</tr>\n";
}
?>
</table>
I'm a noob to php, and I can't get why it's not working... I just want the name of the user to be in the post.
There was an extra period (concat operator) before your session variable. I cleaned it up for you.
echo "<td>" . $_SESSION['user'] . "<p>'s BFFL is</p>" . $list['body'] . "<br/>\n";

Categories