I have php function, whose create some blocks with data from loop.
finction.php:
function drawcomplect() {
for ($i=0;$i<count($new_arr);$i++) {
if ($i%2==0) {
echo "<div class='numeric'>".$j++."</div>";
echo "<div class='item'>".$new_arr[$i]."</div>";
} else {
echo "<div class='count'>".$new_arr[$i]."</div>";
}
}
}
And mPdf file for output:
$html ='<html>'.drawcomplect().'</html>'
If run this function in php file, it's work. But mPdf not work, print "Page error". How I can add data to pdf?
function drawcomplect() {
$str = '';
for ($i=0;$i<count($new_arr);$i++) {
if ($i%2==0) {
$str .="<div class='numeric'>".$j++."</div><div class='item'>".$new_arr[$i]."</div>";
} else {
$str .="<div class='count'>".$new_arr[$i]."</div>";
}
}
return $str;
}
ANd Mpdf:
$html ='<html><body>'.drawcomplect().'</body></html>'
Related
I working on a XML parse script. The XML has more than 12.000 pieces items ("term" the element name) but the script gives only 4489 pieces element. Here my code:
<?php
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->load('theXML.xml');
$i=0;
while(is_object($ajanlatok = $doc->getElementsByTagName("term")->item($i)))
{
foreach($ajanlatok->childNodes as $nodename)
{ echo $i;
if($nodename->nodeName=='dep')
{
echo "<u>Indulás</u><br> Innen: ".$nodename->getAttribute('from')." - Ide: ".$nodename->getAttribute('to')." - Dátum: ".$nodename->getAttribute('date')."<br>";
}
elseif($nodename->nodeName=='prices')
{
foreach($nodename->childNodes as $subNodes)
{
echo $subNodes->nodeName."(ár) - ".$subNodes->nodeValue."<br>";
}
}
elseif($nodename->nodeName=='surcharges')
{
foreach($nodename->childNodes as $subNodes)
{
echo $subNodes->nodeName."(felár) - ".$subNodes->nodeValue."<br>";
}
}
elseif($nodename->nodeName=='rooms')
{
foreach($nodename->childNodes as $subNodes)
{
echo $subNodes->nodeName."(szoba) - ".$subNodes->nodeValue."<br>";
}
}
else
{
echo $nodename->nodeName." - ".$nodename->nodeValue."<br>";
}
}
echo "<hr>";
$i++;
}
?>
Is there any limitation or why I don't get all the content. The XML file large, 70mb.
I was wondering if it's possible to have an if statement within an echo.
I have if statement which works fine when echoing results through the a while loop... This is the statement:
<div><?php if ($row['image'] == '') {}
else {echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";} ?>
<?php if ($row['video'] == '') {}
else {echo "<iframe src={$row['video']}></iframe>";} ?></div>`
So basically it's either a video or an image which works fine but then I implemented an infinite scroll to my blog which echoes the data from the database through and if statement like so:
if ($results) {
while($obj = $results->fetch_object())
{
echo '
<div><h3>'.$obj->headline.'</h3> </div>
<div><img src='data:image/jpeg;base64,".base64_encode('.$obj->image.')."'></div>'
So I wondering if anyone knows if it's possible to transfer that if statement within this echo so that it display an image firstly and then knows whether one is present or when a video is present within the database.
Thanks in advance for any help.
PS: I'm very new to coding/php!
Of course. Just split up the echo into multiple statements:
while($row = $results->fetch_object()) {
echo '<div>';
if ($row['image'] == '') {
} else {
echo "<img src='data:image/jpeg;base64,".base64_encode($row['image'])."'>";
}
if ($row['video'] == '') {
} else {
echo "<iframe src={$row['video']}></iframe>";
}
echo '</div>';
}
Try this one.
//first initialize a variable as a string
$result="";
while($obj = $results->fetch_object()) {
$result.="<div>";
if (!empty($obj['image'])){
$result.="<img src='data:image/jpeg;base64,".base64_encode($obj['image'])."'>";
}
elseif (!empty($obj['video'])){
$result.="<iframe src={$obj['video']}></iframe>";
}else{
//show some notification or leave it
//echo 'not Found';
}
$result.="</div>";
}
//finally you need to print the result variable.
echo $result;
Currently, I have this code:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
}
} else {
echo "Item does not exist!";
}
?>
If "$matches[1]" has nothing in it, I want my code it to echo "Item does not exist!" How do I do this though? Please help!
I've tried something like if ($matches[1]=="") { echo "Item does not exist!"; } before, but it didn't work. This is what I got:
http://img685.imageshack.us/img685/998/28990b2c12d0423292d3574.png
See works fine right? Look what happens if $matches[1] DOES exist:
http://img528.imageshack.us/img528/3690/71472c9de6ec49118ee8d48.png
It still comes up! How can I make my code so it only echos the error if there is nothing for $matches[1]? PLEASE HELP ME!
If you are wondering, this is my code when I added the if ($matches[1]=="") { echo "Item does not exist!"; } in:
<?php
if (isset($_GET['s'])) {
$itemid = $_GET['s'];
$search = "$itemid";
$query = ucwords($search);
echo "<title>Results for $query</title>";
$string = file_get_contents('http://clubpenguincheatsnow.com/tools/newitemdatabase/items.php');
if ($itemid == "") {
echo "Please fill out the form.";
} else {
echo '<div id="content">';
$string = explode('<br>', $string);
foreach ($string as $row) {
preg_match('/^(.+)\s=\s(\d+)\s=\s(\D+)\s=\s(\d+)/', trim($row), $matches);
if (preg_match("/$query/i", "$matches[1]")) {
echo "<a href='http://clubpenguincheatsnow.com/tools/newitemdatabase/info.php?id=$matches[2]'>";
echo $matches[1];
echo "</a><br>";
}
}
echo '</div>';
if ($matches[1] == "") {
echo "Item does not exist!";
}
}
} else {
echo "Item does not exist!";
}
?>
Any help to my question would be VERY HIGHLY APPRECIATED!
Look at PHP's comparison operators
if (empty($matches[1])) { echo "Item does not exist!"; }
else {
echo "Item does not exist!";
}
This "else" is in regards to isset($_GET['s']). Is there a variable called s in your query string? If there isn't, then it's normal that you get the message.
Maybe you put that else block in the wrong place?
I am using this code to get ID3 information of an MP3 File.
<?php
class CMP3File {
var $title;var $artist;var $album;var $year;var $comment;var $genre;
function getid3 ($file) {
if (file_exists($file)) {
$id_start=filesize($file)-128;
$fp=fopen($file,"r");
fseek($fp,$id_start);
$tag=fread($fp,3);
if ($tag == "TAG") {
$this->title=fread($fp,30);
$this->artist=fread($fp,30);
$this->album=fread($fp,30);
$this->year=fread($fp,4);
$this->comment=fread($fp,30);
$this->genre=fread($fp,1);
fclose($fp);
return true;
} else {
fclose($fp);
return false;
}
} else { return false; }
}
}
?>
I am then using this code to call it.
include ("CMP3File.php");
$filename="music_file.mp3";
$mp3file=new CMP3File;
$mp3file->getid3($filename);
echo "Title: $mp3file->title<br>\n";
echo "Artist: $mp3file->artist<br>\n";
echo "Album: $mp3file->album<br>\n";
echo "Year: $mp3file->year<br>\n";
echo "Comment: $mp3file->comment<br>\n";
echo "Genre: " . Ord($mp3file->genre) . "<br>\n";
Is there a way to also get and echo out the album art as an image?
i wrote some code in codeigniter
index controller using model 'navigation':
$this->load->model('navigation');
$data['template']=$this->navigation->nav_template();
the nav_template() function generate string that will be output in brower:
function nav_template($uplevel=0)
{
$tablename=$this->db->dbprefix("test");
$sql="select * from $tablename where id_parent=$uplevel "
$menu_item=$this->db->query($sql);
foreach($menu_item->result_array() as $row)
{
if($something)
{
if
{
echo 'some str';
}
self::nav_template($uplevel);//call self
}
else
{
echo 'other str';
}
}
echo '</ul>';
}
in view file i am using an <?php echo $template ?>
but as we all known , i am using echo() to output string. i wanted to store the string in some php varialbe that can be used in view file template tag.
and the nav_template() function just called itself using self php keyword.
nowhere to define the var like this:
$template=''; and no where to return the string?
so , can anyone tell me how to return the output string within the nav_template() instead of echo it directly to browser?
ps: the output data is the formatted html code that will generate a treeview with help of some javascript scripts.
function nav_template($uplevel=0)
{
$tablename=$this->db->dbprefix("test");
$sql="select * from $tablename where id_parent=$uplevel "
$menu_item=$this->db->query($sql);
$out = "";
foreach($menu_item->result_array() as $row)
{
if($something)
{
$out .= self::nav_template($uplevel);
}
else
{
$out .= 'other str';
}
}
$out.= '</ul>';
return $out;
}