PHP with HTML echo in echo - php

For some reason after my table displays the following extra text: echo;"
I've played with the code a lot but get the same result.
if (mysqli_num_rows($result) > 0)
{
echo "<table>";
echo(
"<tr>"
."<th>"
."Online Users"
.$num_rows
."</th>"
."<th>"
."Offline Users"
."</th>"
."</tr>"
);
while($row = mysqli_fetch_assoc($result)) {
echo(
"<tr>"
."<td>"
.row['name']
."</td>"
."</tr>"
);
}
echo "</table>";
} else {
echo "an error has occurred";
}

The problem is that your server is not parsing the PHP at all; it's returning it to the browser as if it were HTML. You need to do three things:
Make sure you have <?php before the code in your question.
Make sure your file's name ends in .php.
Make sure your server has PHP installed and knows to interpret .php files as PHP, not as HTML.
Many variations of this question have been asked here before, though I couldn't find an exact duplicate. It's not clear from your question which of the above 3 steps is causing your problem.

Related

html_entity_decode nesting php

running php 5.6.29 here.
Processing about 6,000 rows of data from a MYSQL Table. Looking at the HTML Description column and making changes. Using html_entity_decode to show the changed result in the Browser.
The problem is, each row is displaying the HTML result nested inside the previous row, which makes the display impossible. Obviously I want each row to have a clean break. As these are tables, it seems not to break?
while ($row = mysqli_fetch_array($result))
{
$i++;
echo "<br>$i";
echo "<br>Product: ".$row['id'];
echo "<br>Title: ".$row['title'];
echo "<br>";
$a = $row['html'];
echo "htmlspecialchars_decode($a)";
//...change the html process here ...
$b = html_entity_decode($row['html']);
echo "<br><br><br>".$b;
echo "<br>";
echo "<br>";
echo "<br>";
echo "<br>";
}
I checked this in multiple browsers, so it's not just Chrome. Basically I somehow need to close the HTML before I move onto the next row right? Or is this some kind of limitation or bug? tia.

strange thing happens when php displaying in html by form actions

First, I got a html file containing code like this:
<form action="Journey.php" method="POST">
<select name = "Startpoint">
<optgroup label = "Start point">
<option value = "GrimesDyke">GrimesDyke</option>
<option value = "SeacroftRingRoad">SeacroftRingRoad</option>
......
this part work fine, it calld the Journey.php file and pass the right data, i use these data to perform php calculations and want it to be displayed in table format like this
echo "<table id = 'Journey' border='1' style='border-collapse:
collapse;border-color: silver;'>";
echo "<tr style='font-weight: bold;'>";
echo "<td width='150' align='center'>Stops</td>";
echo "</tr>";
foreach ($StopRow as $row)
{
echo '<td width="150" align=center>' .$row. '</td>';
echo '</tr>';
}
in my eclipse-php IDE, this worked just fine, however, when it come to the browser... well, there is a table on display.....along with half of my php code.....like the picture below
the thing is, before i put my php code in the html body tag, it works well....please show me how can i make it right...thank you
If PHP is enabled & working then :
It is possible that you have a ?> at that point in code. Just search
for it, you should find it. Remove it.
It may also be cause if you are accessing the file directly(by file path on the browser),
Use : http://localhost/index.php
Otherwise it may be a config problem and hence the question a duplicate : this answer

Issue echoing links PHP MySQL, missing code

I am having trouble with my code. I want to display posts on one page grouped by category but I also need the titles of the posts to be links to the actual articles, everything I’ve tried so far has broken. Can anyone help? I can echo the titles just not as links!
You should take the excellent advice from #Marten and #CharlesAddis in the comments, and if you don't want to use an ORM, you should at least use PDO.
However, to give an answer for answer's sake:
while ($row = mysqli_fetch_array($result)){
echo "<h2>".$row['category']."</h2>"."<br />";
$titles = explode(", ",$row['titles']);
foreach ($title as $t) {
echo '' . $t . '<br>';
}
echo "<hr />";
}
I'm not sure what the relation between post titles and post links are, if you can add that I can update the answer.

How to php echo content into <div> classes dynamicaly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
My goal is to print all results (fetched from database) and print it according to respective formation.
In html, I could defined it as follows:
<div class="gallery-item category-birds">
<div class="gallery-item-image" style="background-image: url( '_uploads/bc7113a3956b7a45e926f30258fa1b83.jpg' );">
<div class="gallery-item-image-pattern">
<span>Show image</span>
</div>
</div>
<h3>ImageTest1</h3>
<p>MyImageDescription.</p>
</div>
In php, I am trying to print the results in a loop (like I have done before in others examples) but for some reason it's not working.
"animals.php"
<?php
include("../config.php");
$result = mysql_query("SELECT * FROM animals WHERE catid=2 ORDER BY id DESC",$connect);
while($myrow = mysql_fetch_assoc($result))
{//begin of loop
echo "<div class='gallery-item category-birds'>";
echo "<div class=gallery-item-image style='background-image:url(_uploads/bc7113a3956b7a45e926f30258fa1b83.jpg);'>";
echo "<div class=gallery-item-image-pattern>";
echo "<span>Show image</span>";
echo "</div>";
echo "</div>";
echo "<h3> ".$myrow['name']." </h3>";
echo "<p> ".$myrow['description']." </p>";
echo "</div>";
}//end of loop
?>
The index.php includes "animals.php".
The database connection is fine, because by accessing "animals.php" I have the content desired.
The index.php have css linked, where is defined the classes and others.
I have tried to replace inside of those echo's: " for \". -> Not working.
I have tried to replace inside of those echo's: " for '. -> Not working.
Could you help me? Iam out of ideas how to solve this.
Thanks and regards,
RMC
FIrst there is no issue with the MySQL if you can access the page and it works. The issue is when you include it in another page. The issue that comes to mind is if animals.php is located in a folder, example: /inc/. Your config.php and index.php are located in your docroot(/). This may cause a relative issue in loading the config.php file, because it may be looking out of your docroot. I would check your error.log on your server to verify this. I normally in any templated PHP website that I have created, my config.php file would be included in index.php since it would be used by the whole site.
A few potential solutions:
check your error.log to make sure it is finding the config.php file in the right location
move your include for config.php to index.php
read up on php templating, example tutorial - http://chadminick.com/articles/simple-php-template-engine.html
You're missing some single quotes around your class name of div 2 and 3
<?php
include("../config.php");
$result = mysql_query("SELECT * FROM animals WHERE catid=2 ORDER BY id DESC",$connect);
while($myrow = mysql_fetch_assoc($result))
{//begin of loop
echo "<div class='gallery-item category-birds'>";
echo "<div class='gallery-item-image' style='background-image:url(_uploads/bc7113a3956b7a45e926f30258fa1b83.jpg);'>";
echo "<div class='gallery-item-image-pattern'>";
echo "<span>Show image</span>";
echo "</div>";
echo "</div>";
echo "<h3> ".$myrow['name']." </h3>";
echo "<p> ".$myrow['description']." </p>";
echo "</div>";
}//end of loop
?>
(P.s. I also assume that you make your connection with the database in your config.php file,
what makes that you don't need to put your $connect variable in the mysql_query() function.
$result = mysql_query("SELECT * FROM animals WHERE catid=2 ORDER BY id DESC");
)

PHP If statement for file_exists for image or video content from database to avoid blank space on website when no data is present

Having an issue with a blog webpage I’m trying to create which includes a database. The database contains 3 columns: 'title' 'content' which are both text and 'media' which is a mediumblob for images.
<?php echo "img src='data:image/jpeg;base64,".base64_encode($row['media'])."'>"; ?>
Everything works fine except when I don't include an image (in the data row) and just include a 'title' & 'content' it leaves a blank square on the page where an image should of been.
So firstly any advice towards an if statement (or something like that) that would check if file_exists (for specifically images) and display it, else exclude <img> (to avoid the blank square).
Something like this (which I know is totally wrong, sorry):
<?php
if (file_exists($row['media'])) { echo
"?php echo "img src='data:image/jpeg;base64,".base64_encode($row['media'])."'>"; ?>"
;}
else { echo
"!--?php echo "img src='data:image/jpeg;base64,".base64_encode($row['media'])."'>"; ?>-->"
;}
?>
Ideally with this blog, every entry will contain EITHER an image OR an embedded video via iframe, which I assume is something like this:
<iframe src="?php echo $emeddedLink; ?>"</iframe> (Haven't tried it as yet)
But I don't want a blank <iframe> showing on the website when using an image for the blog entry and vice versa I don't want a blank <img> box when using an embedded video.
Please any help would be greatly appreciated and apologies in advance for what might seem like a very basic question but I’ve been searching and can't seem to find anything really to avoid the blank square when I don't include an image in a data row... Thanks!
Typically you'd have an IF statement within your loop that spits out the list of content from your database. Something like this.
<?php
$sql = '
SELECT
*
FROM
`my_table`
';
$query = mysql_query($sql) OR die(mysql_error());
print '<table>';
print '<tr><td>Title</td><td>Content</td><td>Media</td></tr>';
while ($row = mysql_fetch_assoc($query)) {
print '<tr>';
print '<td>'. $row['title'] .'</td>';
print '<td>'. $row['content'] .'</td>';
print '<td>'
if (
$row['media'] == '' ||
$row['media'] == null ||
empty($row['media']) ||
!$row['media']
) {
print 'No Media Available';
}
else {
print $row['media'];
}
print '</td>';
print '</tr>';
}
print '</table>';
?>
That should get you going if I understand what you want to do. I tried to format the code to be as easy to read for a beginner as possible. The lines that would most closely pertain to your issue above are these lines here:
if (
$row['media'] == '' ||
$row['media'] == null ||
empty($row['media']) ||
!$row['media']
) {
print 'No Media Available';
}
else {
print $row['media'];
}
I put all the contingencies within the if statement because I'm not sure what you have going on in your database. The only thing I think you'll need to change, given the fact that the image is stored as a blob in your database, is instead of printing out the $row['media'] directly, you'd have to parse it and create an image using whatever strategy you see fit for your project.
Using your code above as an example, it would look like this:
if (
$row['media'] == '' ||
$row['media'] == null ||
empty($row['media']) ||
!$row['media']
) {
print 'No Media Available';
}
else {
print '<img src="data:image/jpeg;base64,'. base64_encode($row['media']) .'"/>';
}
Hope this helps!
You can simply add an if statement into the template to check whether the value isn't empty
<?php if(!empty($media)) { ?>
<img src="<?php echo $media ?>">
<?php } ?>

Categories