Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
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.
Improve this question
I have stupid problem with the html/php rules. I'm trying to show an image from an apache server with this code using a table:
<?php
//code
while($row = mysqli_fetch_array($result)) {
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src=\"<?php echo $url; ?>\"/></td>
</tr>';
}
//code
?>
But obviusly the inner php script is considered as normal text and no run!
Don't echo large blocks of HTML in PHP like that. Its bad practice. No, actually, its horrible practice. Instead learn to open and close the PHP tag as needed, like:
<?php
//code..code...code...
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td> <?php echo $row['x']; ?> </td>
<td> <?php echo $row['y']; ?> </td>
<td> <?php echo $row['z']; ?> </td>
<td> <?php echo $row['f']; ?> </td>
<td> <?php echo $row['g']; ?> </td>
<td> <?php echo $row['d']; ?> </td>
<td><img src="<?php echo $url; ?>"/></td>
</tr>
<?php
}
//code..code...code
?>
There are several benefits to this, including that its less likely to break syntax highlighting and your code is not defaced with as many \" all over the place.
You're already inside PHP - you shouldn't open another <?php scope:
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="' .$url . '"/></td>
</tr>';
Just change this:
<td><img src=\"<?php echo $url; ?>\"/></td>
to:
<td><img src="' . $url .'"/></td>
Try it:
<?php
while($row = mysqli_fetch_array($result)){
echo '
<tr>
<td> '.$row['x'].' </td>
<td> '.$row['y'].' </td>
<td> '.$row['z'].' </td>
<td> '.$row['f'].' </td>
<td> '.$row['g'].' </td>
<td> '.$row['d'].' </td>
<td><img src="'.$url.'"/></td>
</tr>';
}
?>
It just a problem in concatenation, you need just combine those strings, not set another 'echo script'.
you can simply close the php section and put plain html, then reopen php when needed like this :
<?php
$test = array( "a test","also","a","test");
$itteraror = 0;
$url = "#";
?>
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-GB" lang="en-GB">
<head>
</head>
<body>
<table>
<?php
while($itteraror<sizeof($test)) {
?>
<tr>
<td> <?php echo $test[$itteraror] ; ?></td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td> <?php echo $test[$itteraror] ; ?> </td>
<td><img src="<?php echo $url ; ?>" alt="my image"></td>
</tr>
<?php
$itteraror++;
}
?>
</table>
</body>
</html>
i did make some modification to your code in order for it to be standalone for testing purpose.
Related
So I have some code that looks like this
<tr>
<td>
<?php echo "text_". $textnum ?>
</td>
</tr>
And I want the code to function the same as
<tr>
<td>
<?php echo $text_x ?>
</td>
</tr>
Where X is the value of $textnum. I cannot just use <?php echo $text_1 ?> because I dont know which text variable i am going to be echoing
Use <?=${'text_'.$textnum};?> or <?php echo ${'text_'.$textnum} ?>
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
As you can see in the picture below, the name of the picture file appears after every image. I dont want the image name to be displayed. Is there any way of hiding the name so per.jfif, ola.jfif and arne.jfif doesnt appear?
I have been searching on here for an answer, but it doesnt seem to have been asked on here before. Thanks for the help in advance,
Below this text you can see my messy code.
<?php
$tilkobling = mysqli_connect("localhost","root","","oppgave_normalisering");
$sql = "SELECT person_table.personr, person_table.navn, person_table.adresse, person_table.mobilnr, person_table.postnr, person_table.bilde, mobil_table.model, sted_table.sted FROM mobil_table, person_table, sted_table WHERE person_table.personr = sted_table.stednr AND person_table.personr = mobil_table.modelnr";
$datasett = $tilkobling->query($sql)
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Startside</title>
<link rel="stylesheet" href="startside.css">
</head>
<body>
<nav>
<ul>
<li>Per</li>
<li>Ola</li>
<li>Arne</li>
</ul>
</nav>
<table >
<tr>
<td style="text-align:center"><b>Nr<b></td>
<td style="text-align:center"><b>Navn<b></td>
<td style="text-align:center"><b>Adresse<b></td>
<td style="text-align:center"><b>Postnr</b></td>
<td style="text-align:center"><b>Sted</b></td>
<td style="text-align:center"><b>Mobilnr</b></td>
<td style="text-align:center"><b>Modell</b></td>
<td style="text-align:center"><b>Bilde</b></td>
</tr>
<?php while ($rad = mysqli_fetch_array($datasett)) { ?>
<tr>
<td style="text-align:center"><?php echo $rad["personr"]; ?></td>
<td style="text-align:center"><?php echo $rad["navn"]; ?></td>
<td style="text-align:center"><?php echo $rad["adresse"]; ?></td>
<td style="text-align:center"><?php echo $rad["postnr"]; ?></td>
<td style="text-align:center"><?php echo $rad["sted"]; ?></td>
<td style="text-align:center"><?php echo $rad["mobilnr"]; ?></td>
<td style="text-align:center"><?php echo $rad["model"]; ?></td>
<td><img src="<?= $rad['bilde'] ?>" alt="" style="height:100px; width:140px;" id="bilde"> <?php echo $rad["bilde"]; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
You print image name in your last td. Just remove <?php echo $rad["bilde"]; ?>.
<td>
<!-- ↓↓ this is image element -->
<img src="<?= $rad['bilde'] ?>" alt="" style="height:100px; width:140px;" id="bilde">
<!-- ↓↓ delete this ↓↓ -->
<?php echo $rad["bilde"]; ?>
</td>
Dont Need This part
<?php echo $rad["bilde"]; ?>
I generating a QR CODE with url as data content, but when i try to print a page where the qr is generated, the qr does not seem to appear yet other content displays. Here is my snippet
<?php
require_once("qrcode.php");
//---------------------------------------------------------
$qr = new QRCode();
// ƒGƒ‰[’ù³ƒŒƒxƒ‹‚ðÝ’è
// QR_ERROR_CORRECT_LEVEL_L : 7%
// QR_ERROR_CORRECT_LEVEL_M : 15%
// QR_ERROR_CORRECT_LEVEL_Q : 25%
// QR_ERROR_CORRECT_LEVEL_H : 30%
$qr->setErrorCorrectLevel(QR_ERROR_CORRECT_LEVEL_L);
$qr->setTypeNumber(4);
$qr->addData("http:/".$_SERVER['REQUEST_URI']."");
$qr->make();
//---------------------------------------------------------
?>
and below is the other content on the page
<div class="invoice-box" id="invoice-box">
<table cellpadding="0" cellspacing="0">
<tr class="top">
<td colspan="2">
<table>
<tr>
<td class="title">
<img src="images/logo.png" style="width:100%; max-width:200px; height:95px;">
</td>
<td>
Invoice #: <?php print $invoice_details->sub_code ?><br>
Created: <?php print date('Y/M/d', strtotime($invoice_details->paid_date)) ?><br>
Due: February 1, 2015
</td>
</tr>
</table>
</td>
</tr>
<tr class="information">
<td colspan="2">
<table>
<tr>
<td>
<br>
<br>
</td>
<td>
<?php print $invoice_details->org ?><br>
<?php print $invoice_details->lname ?> <?php print $invoice_details->fname ?><br>
<?php print $invoice_details->email ?>
</td>
<td>
<?php $qr->printHTML(); ?>
</td>
</tr>
</table>
</td>
</tr>
<tr class="heading">
<td>
Payment Method
</td>
<td>
</td>
</tr>
<tr class="details">
<td>
<?php print $invoice_details->method ?>
</td>
<td>
</td>
</tr>
<tr class="heading">
<td>
Item
</td>
<td>
Price(UGX)
</td>
</tr>
<tr class="item">
<td>
<?php print ucfirst($invoice_details->event) ?> - Summit
</td>
<td>
<?php print number_format($invoice_details->amount) ?>
</td>
</tr>
<tr class="total">
<td></td>
<td>
Total: UGX <?php print number_format($invoice_details->amount) ?>
</td>
</tr>
</table>
</div>
<input type="button" value="Print Div" onclick="PrintElem('#invoice-box')" />
and my printing script
<script type="text/javascript">
function PrintElem(elem)
{
Popup($(elem).html());
}
function Popup(data)
{
var mywindow = window.open('', 'invoice-box', 'height=400,width=600');
mywindow.document.write('<html><head><title>my div</title>');
/*optional stylesheet*/ //mywindow.document.write('<link rel="stylesheet" href="main.css" type="text/css" />');
mywindow.document.write('</head><body >');
mywindow.document.write(data);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10
mywindow.print();
mywindow.close();
return true;
}
</script>
Assuming I found the correct library, it has an image creator. What you need to do is use an output buffer around the imagegif (or imagepng) call to capture the image's binary data into a string, then base64 encode the string, and echo that as part of a data URI directly in the src attribute of an img tag.
$img = $qr->createImage(4, 2);
ob_start();
imagegif($img);
imagedestroy($img);
$img = ob_get_clean();
In your HTML
<img src="data:image/gif;base64,<?php echo base64_encode($img);?>">
If you're concerned about support for data URLs, leave out the output buffering, add a unique, random filename as the second parameter to the imagegif call to save it, then echo that filename in the src.
It seems that you're trying to print a black background color which is removed by default from printing. Unfortunately there's no way to force it back programatically in browsers other than Safari and Chrome.
You can force this back for Safari and Chrome by doing:
<div class="invoice-box" id="invoice-box">
<style>
.invoice-box table td: {
-webkit-print-color-adjust: exact !important;
}
</style>
<table cellpadding="0" cellspacing="0">
<tr class="top">
<td colspan="2">
<table>
<tr>
<td class="title">
<img src="images/logo.png" style="width:100%; max-width:200px; height:95px;">
</td>
<td>
Invoice #: <?php print $invoice_details->sub_code ?><br>
Created: <?php print date('Y/M/d', strtotime($invoice_details->paid_date)) ?><br>
Due: February 1, 2015
</td>
</tr>
</table>
</td>
</tr>
<tr class="information">
<td colspan="2">
<table>
<tr>
<td>
<br>
<br>
</td>
<td>
<?php print $invoice_details->org ?><br>
<?php print $invoice_details->lname ?> <?php print $invoice_details->fname ?><br>
<?php print $invoice_details->email ?>
</td>
<td>
<?php $qr->printHTML(); ?>
</td>
</tr>
</table>
</td>
</tr>
<tr class="heading">
<td>
Payment Method
</td>
<td>
</td>
</tr>
<tr class="details">
<td>
<?php print $invoice_details->method ?>
</td>
<td>
</td>
</tr>
<tr class="heading">
<td>
Item
</td>
<td>
Price(UGX)
</td>
</tr>
<tr class="item">
<td>
<?php print ucfirst($invoice_details->event) ?> - Summit
</td>
<td>
<?php print number_format($invoice_details->amount) ?>
</td>
</tr>
<tr class="total">
<td></td>
<td>
Total: UGX <?php print number_format($invoice_details->amount) ?>
</td>
</tr>
</table>
</div>
<input type="button" value="Print Div" onclick="PrintElem('#invoice-box')" />
For browsers like IE and Firefox unfortunately this requires user interaction. See this question for more answers: Printing background-color in Firefox and IE
Finally, i got a way around this, here is my solution
<?php
require_once("qrcode.php");
$qr = QRCode::getMinimumQRCode("http:/".$_SERVER['REQUEST_URI']."", QR_ERROR_CORRECT_LEVEL_L);
$img = $qr->createImage(4, 2);
ob_start();
imagegif($img);
imagedestroy($img);
$img = ob_get_clean();
?>
Then output the qrcode as image with this;
<img src="data:image/gif;base64,<?php echo base64_encode($img);?>">
Thanks to #walf for his solution somehow, hope it helps someone else.
I have stumbled upon a bit of a issue and I'm hoping that I can get some help/pointer here how to overcome it.
I have an external xml file that I need to parse and display in the table. I did this.
The issue is that the file is updating every second, so I would like the data in the cells to update "live" as well, according to the changes in xml file.
The problem is that I don't know what should I use to do it. Can anyone please point me into right direction.
<?php
require('simple_html_dom.php');
$html = file_get_html('http://admin:admin#192.168.1.108/status.xml');
foreach($html->find('response') as $row) {
$frontdoorlock = $row->find('led1',0)->plaintext;
$reardoorlock = $row->find('led2',0)->plaintext;
$fronthandle = $row->find('btn0',0)->plaintext;
$frontdoor = $row->find('btn1',0)->plaintext;
$rearhandle = $row->find('btn2',0)->plaintext;
$reardoor = $row->find('btn3',0)->plaintext;
$temperature = $row->find('temp0',0)->plaintext;
$humidity = $row->find('humid0',0)->plaintext;
$currentdate = $row->find('temp2',0)->plaintext;
$lastpowered = $row->find('temp3',0)->plaintext;
}
?>
<div>
<table>
<tr>
<td>
Cabinet Name
</td>
<td >
Front Door Lock
</td>
<td>
Front Handle
</td>
<td>
Front Door
</td>
<td>
Rear Door Lock
</td>
<td>
Rear Handle
</td>
<td>
Rear Door
</td>
<td>
Temperature
</td>
<td>
Humidity
</td>
<td>
Current Date
</td>
<td>
System Last Powered
</td>
</tr>
<tr>
<td>
Cab1
</td>
<td >
<?php if($frontdoorlock == "0") echo "CLOSED";
else echo "OPEN";
?>
</td>
<td>
<?php if($fronthandle == "0") echo "CLOSED";
else echo "OPEN";
?>
</td>
<td>
<?php if($frontdoor == "0") echo "OPEN";
else echo "CLOSED";
?>
</td>
<td>
<?php if($reardoorlock == "0") echo "CLOSED";
else echo "OPEN";
?>
</td>
<td>
<?php if($rearhandle == "0") echo "CLOSED";
else echo "OPEN";
?>
</td>
<td>
<?php if($reardoor == "0") echo "OPEN";
else echo "CLOSED";
?>
</td>
<td>
<?php echo $temperature; ?>
</td>
<td>
<?php echo $humidity; ?>
</td>
<td>
<?php echo $currentdate ?>
</td>
<td>
<?php echo $lastpowered ?>
</td>
</tr>
If you don't wan't to change the logic of the page itself ... like doing everything with javascript. You can simply put a:
<META HTTP-EQUIV="refresh" CONTENT="15">
meta tag in your HTML ... then the browser will update the page every X seconds (being the CONTENT value).
This being the simplest solution for the current situation.
But I would highly suggest to do this with JavaScript. There a tons of frameworks that will do the hard stuff for you.
My personal favourite: VueJS
Have you considered a front-end framework like angularJs that supports data binding? Data-binding is incredibly powerful and I feel like it would be the perfect solution for your problem. Here's a quick video demonstrating it.
You can bind your output table to a variable that represents that data, which updates each second according to the XML.
I have a couple of problems. I'm creating a form inside a table, from what I understand this is not a good idea. But because of the loop I want to make sure the table header is outside so it doesn't repeat. Is there a smarter way to do this?
Also more importantly I can't seem to get the delete button to remove the correct video. It seems to delete the last one in the list. Something wrong with how I'm looping over this?
<p>
<h3>Recorded Videos</h3>
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
for($i=0;$i<$num_videos;$i++)
{
<form action="<?php htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="<?php echo $result_videos[$i]["video_id"]; ?>" />
<tbody>
<tr>
<td>
<?php echo $result_videos[$i]["camera_name"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_size"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_datetime"]; ?>
</td>
<td>
<?php echo $result_videos[$i]["video_length"]; ?>
</td>
<td>
<input type="submit" name="delete_video" value="Delete" onClick="javascript:return confirm('Delete this video?');"/>
</td>
</tr>
</tbody>
}
echo "</table>";
echo "</form>";
echo "</p>";
}
}
if (isset($_POST['delete_video'])) {
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']);
In your loop you are creating the 'form' tag. However you are not closing it each time. This is causing your deleting problem.
Move
echo "</form>";
Inside the loop.
looks good to me, the only issue I see is that the tag should be outside the loop (opening before, closing after).
Revised code
<?
if (isset($_POST['delete_video']))
{
$video_id = $_POST['video_id'];
$query_delete_video = 'DELETE FROM `#__videos` WHERE `video_id`='.$video_id;
$db->setQuery($query_delete_video);
$db->query();
header("location: " . $_SERVER['REQUEST_URI']); //you should not allow things to be echoed before a header()
}
?>
<script type="text/javascript">
function onMySubmit(video_id)
{
document.myform.video_id.value = video_id;
return confirm('Delete this video?');
}
</script>
<p>
<h3>Recorded Videos</h3>
<!-- you had <?php htmlentities(...) ?>, you should have had
<?php echo htmlentities(...) ?> or <?= ... ?> -->
<form name="myform" action="<?= htmlentities($_SERVER['PHP_SELF']);?>" method="POST">
<input type="hidden" name="video_id" value="" />
<table id="webcam-table">
<thead>
<tr>
<td>Camera Name</td>
<td>Video Size</td>
<td>Date Created</td>
<td>Video Length</td>
<td>Video Options</td>
</tr>
</thead>
<tbody>
<? for($i=0;$i < $num_videos;$i++) { ?>
<tr>
<td><?= $result_videos[$i]["camera_name"]; ?></td>
<td><?= $result_videos[$i]["video_size"]; ?></td>
<td><?= $result_videos[$i]["video_datetime"]; ?></td>
<td><?= $result_videos[$i]["video_length"]; ?></td>
<td><input type="submit" name="delete_video" value="Delete" onClick="javascript:return onMySubmit(<?=$result_videos[$i]["video_id"];?>);"/></td>
</tr>
<? } ?>
</tbody>
</table>
</form>
</p>