Return Seperate PHP Script Within HTML - php

I have a "print.php" script that basically fetches data from MySQL and creates a tiny pretty HTML table.
echo "
<table cellpadding=\"2\" cellspacing=\"2\" border=\"0\" width=\"10%\">
<tr bgcolor=\"#666666\">
<td colspan=\"2\" align=\"center\"><b><font color=\"#FFFFFF\">" . $table[0] . "</font></td>
</tr>
<tr>
<td>Name</td>
<td>Score</td>
</tr>";
echo "<tr";
if ($i % 2 == 0)
echo " bgcolor=\"#CCCCCC\"";
echo ">
<td>" . $col['player'] . "</td>
<td>" . $col['score'] . "</td>
</tr>";
I want this table to appear in my index.html, But its in a separate php script.
I want to keep the php script separate from the HTML because the script is not only a tad large, but it has my SQL information in it to which I don't want in the plain HTML.
Is there a way to fetch this echo in my php script from my html page?

Option 1:
Use an iframe. Works even if JavaScript is turned off.
<iframe src="print.php"></iframe>
Option 2: Use AJAX (with jQuery).
Download jQuery here.
Include it in your index.html, preferably in your head tag.
<script type="text/javascript" src="path/to/jquery.js"></script>
Add this code where you want print.php to be included.
<div id="container"></div>
<script type="text/javascript">
$(function(){
$("#container").load("print.php");
});
</script>
Option 3: Go the PHP route. May break other code since you need to rename the index file.
Rename your index.html to index.php and use this code:
<?php include("print.php"); ?>

You can use AJAX Javascript to do that.
Documentation And Examples

You can make a AJAX call to the print.php from the your current page and fetch the data and show them.
$.ajax({
url: "print.php"
}).done(function( html ) {
$("#results").append(html); //results is the div id
});
You will have to get the basic jQuery knowledge for this.
Else you can use .load method which is also a AJAX method.
<div id="results"></div>
$("#results").load("print.php");

Related

Convert a parse ini to array / objects

I'm using a program I made to scrape a file for text and parse the information into a ini file. I asked a question here earlier asking about parse_ini_file and why I could not use the indexes and someone gave me a way to loop through using this.
$ini_array = parse_ini_file("myfile.ini", true
foreach($ini_array as $key=>$value)
I then echo out the result echo ($value['Username']) . " "; and store it into the table. I have the table functioning link here, http://liveviewtest.byethost7.com/index.php but the plugin I'm using is not functioning as intended. The sorting does not work, and I'm wondering is it because the data type? http://www.datatables.net/manual/data tells you that the accepted data types are arrays, objects, and instances. My question is what would be the best way for me to convert this into one of these data types? I'm new to php / html so sorry if this is basic.
Php File http://pastebin.com/VRUHLdMQ
Ini File http://pastebin.com/yZsg16qA
Your table is being generated so PHP & Ini file are fine but HTML code you are generating is incorrect so either DataTable plugin or jQuery DOM parser is confused.
Please fix it and it should work fine.
Put jquery+datatables js/css into <head> section.
You use multiple <tbody> and you put </tr> after </tbody>
So basically you shoud put echo "</tbody>"; outside loop so
echo "<tbody>"; // <<< HERE
foreach ($ini_array as $key => $value) {
echo "<tr>";
for ($i = 0; $i < 12; $i++) {
echo "<td>";
echo ($value[$getstats[$i]]);
echo "</td>";
}
echo "</tr>";
}
echo "</tbody>"; // <<< HERE
I'm wondering is it because the data type? http://www.datatables.net/manual/data tells you that the accepted data types are arrays, objects, and instances.
No, your code its fine as manual says Data can be read from DOM. You need arrays/objects/instances if you create table straight from javascript.
See below:
DOM
When DataTables starts up, it will automatically check the table it is operating on for data that already exists inside it and use it for the table (note that it will throw this data away if you pass in data using data or ajax to get new data!). This is the simplest method of using DataTables - working with a regular HTML table.
Note that when using a DOM sourced table, DataTables will use arrays as the data source (see above) by default, although you could use the columns.data option to have it construct objects for the row data instead.
http://www.datatables.net/manual/data#DOM
Your table structure was not correct :
<html>
<head>
<!-- DataTables CSS -->
<link rel="stylesheet" type="text/css" href="http://liveviewtest.byethost7.com/DataTables-1.10.9/media/css/jquery.dataTables.css">
<!-- jQuery -->
<script type="text/javascript" src="http://liveviewtest.byethost7.com/DataTables-1.10.9/media/js/jquery.js"></script>
<!-- DataTables -->
<script type="text/javascript" src="http://liveviewtest.byethost7.com/DataTables-1.10.9/media/js/jquery.dataTables.js"></script>
<!--<link rel="stylesheet" type="text/css" href="http://liveviewtest.byethost7.com/style.css">-->
</head>
<body>
<script type="text/javascript">
$(document).ready(function () {
$('#mytable').DataTable();
});
</script>
<table id="mytable" border ="1" style="width:100%">
<thead>
<tr>
<th>Name</th>
<th>Kills</th>
<th>Death Total</th>
<th>Suicides</th>
<th>Bears</th>
<th>Wolves</th>
<th>Helicopter</th>
<th>Falling</th>
<th>Cold</th>
<th>Explosions</th>
<th>Barricade</th>
<th>Player Deaths</th>
</thead>
<tbody>
<tr>
<td>hpnotiq</td>
<td>0</td>
<td>1</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td>
<td>0</td><td>1</td><td>0</td><td>0</td></tr><tr><td>TacticalNuke735</td><td>1</td><td>12</td><td>12</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>.buckisM</td><td>2</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Mrs Phluffy</td><td>0</td><td>4</td><td>1</td><td>1</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>My buddy's goin'a jail</td><td>6</td><td>134</td><td>126</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>8</td></tr><tr><td>marble</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Sir Joe</td><td>0</td><td>5</td><td>2</td><td>0</td><td>0</td><td>2</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>StryX</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Haroth</td><td>6</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Mr Sephy</td><td>2</td><td>6</td><td>4</td><td>0</td><td>0</td><td>1</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Solblade531</td><td>2</td><td>4</td><td>2</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Vavbro</td><td>3</td><td>14</td><td>8</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>6</td></tr><tr><td>Heisenberg</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>アッパーおっぱい</td><td>3</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td></tr><tr><td>Tallsockboy</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>CHAPYSIX</td><td>1</td><td>2</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Bik</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Totes MiGoats</td><td>0</td><td>8</td><td>5</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>2</td></tr><tr><td>Blunted Out'cha Mind</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Jubby</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>That0neGuy</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Nuclear</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>michaelswansey</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>kapiowai</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Kami</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Pika</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>WC Visuals</td><td>0</td><td>2</td><td>2</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>STUDNASTY</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>masternubb</td><td>0</td><td>2</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Bignubbb</td><td>2</td><td>5</td><td>4</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>BAAABBYNUBB</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Squirt</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Fluffy</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>| Albatross</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>inmate #42069 weston gayboy</td><td>10</td><td>9</td><td>5</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>3</td></tr><tr><td>isaiahyo</td><td>0</td><td>8</td><td>3</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>2</td><td>3</td></tr><tr><td>Vortex</td><td>1</td><td>5</td><td>3</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>1</td></tr><tr><td>General Pro</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Ecchi Sketchy</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>a e s t h e t i c</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>TheS1mpleGuy2Know</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>♥JaPierDoLe™</td><td>0</td><td>1</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>JaidenV</td><td>1</td><td>2</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>FaceEatingTumor</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>Jfrisk</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Kilgore</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>1</td></tr><tr><td>chilmonik</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>chrishawsome</td><td>0</td><td>1</td><td>0</td><td>0</td><td>1</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>s4MPL3</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td>Dan Gleasak</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td><td>0</td></tr><tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr></tbody>
</table>
</body>
</html>

In PHP use 'img src' as part of data attribute

I am creating a table using both Bootstrap and PHP, which makes use of Popovers. I would ideally like my popover to include a JPG but as I am already using echo to create my html table I am unsure how to incorporate it. I have tried to include the usual 'img src' as shown below but it's just printing out the code.
echo "<td colspan='$newlength' id='example' rel='popover' data-content='<img src='imagetouse.jpg' /> This is my content.' data-title=' This is my Title'>$name . $time2</td>";
The above code terminates the tags after the image src. I am unable to use "" around my image because they surround the whole tags for the echo therefore again terminating the code.
Having attempted nietonfir's suggestion:
echo '<td colspan="' . $newlength . '" id="example" rel="popover" data-content="<img src=\'imagetouse.jpg\' /> This is my content." data-title="This is my Title">' . $name . $time2 . '</td>';
The content of my popover now reads as opposed to displaying the image:
<img src='imagetouse.jpg' /> This is my content.
For one I think that you should move the image to the content attribute. And I couldn't find a description to "original-title", so I assume you just meant title. According to the documentation you can insert HTML into the popover by setting an attribute. See this jsBin for a demo.
<div id="foo" data-html="true" data-content="<img src='http://placehold.it/200x100' /> Content" data-title="Title!">Click</div>
[edit]
Concerning your wrong output statement, the following snippet might fix your issue (please be aware that I didn't incorporate the necessary data-html="true" change for the popover to work):
echo '<td colspan="' . $newlength . '" id="example" rel="popover" data-content="<img src=\'imagetouse.jpg\' /> This is my content." data-title="This is my Title">' . $name . $time2 . '</td>';
Imho you should always terminate your strings and concatenate them. In the case of DOM output I'd even terminate the parser and output the variables individually like
?>
<td colspan="<?php echo $newlength; ?>" id="example" rel="popover" data-content="<img src='imagetouse.jpg' /> This is my content." data-title="This is my Title"><?php echo $name . $time2;?></td>
This way it is easier to spot mistakes for one. And on the other side your editor can highlight your code as what it is: HTML.

How is this PHP while loop code creating new table rows?

You can see that I have a 3 element array. I am using a while loop to then print out all 3 values into a table, one row for each of the three values, but I do not understand how three rows are being printed out when I only have one row hard coded using html code. The PHP while loop does not echo the tr and td tags for each row because those row and detail tags are outside the PHP code. The code works -- it prints out one additional new table row for each value of "mary","donna","shirley", but I do not understand how. I could see it working if the tr and td tags were output by a PHP echo statement inside the while loop, but that is not the case here.
<html>
<body>
<table cellspacing ="2" cellpadding ="2" align ="center" border="8">
<?php
$ar1=["mary","donna","shirley"];
$len=count($ar1);
$ct=0;
?>
<?php while ( $ct<$len) { ?>
<tr>
<td>
<?php echo $ar1[$ct];
$ct++;
?>
</td>
</tr>
<?php } //end while loop?>
</table>
</body>
</html>
I believe the answer lies in the fact that HTML is an interpreted language not a compiled language.
So in this case your php while loop is setting the browser back to the spot just before your first tr tag so it goes through and interprets those tags again, and as it does this it puts them to the page again. Doing your while loop like this is a cheap way to do echos essentially.
I'm not a PHP master by any means, but from my understanding of how HTML is read and how PHP works this is my answer.
Your <tr> tag is inside the while loop. If you want just one row, try this-
<html>
<body>
<table cellspacing ="2" cellpadding ="2" align ="center" border="8">
<?php
$ar1=["mary","donna","shirley"];
$len=count($ar1);
$ct=0;
?>
<tr>
<?php while ( $ct<$len) { ?>
<td>
<?php echo $ar1[$ct];
$ct++;
?>
</td>
<?php } //end while loop?>
</tr>
</table>
</body>
</html>
This piece of code:
<?php while ( $ct<$len) { ?>
<tr>
<td>
<?php echo $ar1[$ct];
$ct++;
?>
</td>
</tr>
<?php } //end while loop?>
is the same as
<?php while ( $ct<$len) {
echo "<tr>
<td>";
echo $ar1[$ct];
$ct++;
echo "</td>
</tr>";
} //end while loop?>
if you analyze code more deeply..u will understand it yourself..you said "The php while loop does not echo the tr and td tags for each row because those row and detail tags are outside the php code"
but it does ..the html code is not the part of php code it coded outside php scope...and whenever your while loop executes it again reads the tr and tg tag and insert row and hence u get three rows printed ...
It's an easy case of PHP basic capabilities.
See http://php.net/manual/en/language.basic-syntax.phpmode.php
Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content.
PHP parser, don't need to know what is inside the While Loop, it just repeat to the output.
In a php file, by escaping the php code (by way of ?> you basicly say to the script, now comes something non php, you provide Html tags which are then interpreted by the browser as html code. But because you're still in the loop (you haven't ended it by adding an closing tag } you repeat the exit from the code, presenting html, and then entering the code again.
Some coders prefer to just exit php code and to show some html code with a few php tags here and there when there's a large amount of html being displayed. It's a lot less typing than continually using echo statements.

how to write html code inside php?

I want to write some html code in php. In this html code I am calling a javascript function. but while it is calling the function there is a problem. I think the problem is quotes but I couldn't fix it.
This is the html code that I want to put inside php.
<table>
<tr>
<img src="s.png" name="img">
</tr>
</table>
and this is my javascript code;
<script type="text/javascript">
img1 = "s.png";
img2 = "k.png";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
}
</script>
How can i write this html inside php code?
Thanks
<?php
// your php code
?>
<table>
<tr>
<img src="s.png" name="img">
</tr>
</table>
<?php
// your php code
?>
<script type="text/javascript">
img1 = "s.png";
img2 = "k.png";
function chng(c_img) {
if (document[c_img].src.indexOf(img1)!= -1) document[c_img].src = img2;
else document[c_img].src = img1;
}
</script>
<?php
you also could wrap your code in heredoc, and echo it afterwards http://www.php.net/manual/de/language.types.string.php#language.types.string.syntax.heredoc
you could echo the html; eg
echo "<table>
<tr>
<img src=\"s.png\" name=\"img\">
</tr>
</table>
";
Just escape the double quotes with a backslash.
You can use PHP heredoc syntax:
var $js = <<<JS
// code
JS;
Escapes and echo "<html_code>" is a noob style.
Use scriptlets and the command echo.
<?
echo "<script type=\"text\/javascript\">"
?>
and so far. But pay attention of masking quotations signs and backslashes, etc.
Write html tags inside php code isn't nice. Read about templates to php as Smarty
http://www.smarty.net/

jQuery Tabs - jquery script only works for 1st tab - bad initialize?

I have jQuery Tabs in which each tab and corresponding content are populated by a PHP foreach loop.
For each tab the user can upload a picture. I have this setup in a way so that as soon as the user chooses a file, a jquery script makes the upload begin automatically -- and the browse button is hidden and replaced by "Uploading your picture".
Here is the code (CI markup):
<ul class="tab_header">
<?php foreach ($p as $row):
echo '<li>
' . $row->p_name . '
</li>';
endforeach; ?>
</ul>
<?php foreach ($p as $row): ?>
<div id="tabs-<?php echo $row->p_id; ?>">
<?php echo form_open_multipart('/p/p_upload_picture/' . $row->p_id, 'id="upload_form"');
echo form_upload('userfile', '', 'id="file_select"');
echo form_hidden('upload','upload');
echo form_close(); ?>
<div id="loading" style="display:none;">
Uploading your picture...
</div>
</div>
<?php endforeach; ?>
<script>
$(function(){
$("#file_select").change(function(){
$("#upload_form").submit();
$("#upload_form_div").hide();
$("#loading").show();
});
});
</script>
This works perfectly but only on the first tab.
In all other tabs, the jquery script doesn't run -- as soon as I choose a file, the file name is shown in the browse button field instead, and no upload happens (form isn't submitted).
I wonder if this has to do with the script not being initialized for the other tabs.
How can I fix this?
Thanks for helping, much appreciated.
Your field has an id. Which should be unique per element.
jQuery only returns the first element it encounters with the given id, that's why your code isn't working. I would suggest adding a class instead of an id to your field.
$('.class-of-field')..change(function() {
..
});
The above should do the job.
This would be the equivalent code without jQuery:
var el = document.getElementById('file_select');
Which only returns one element, have a look at the documentation.

Categories