Print content of a div inside a foreach loop - php

I have a foreach loop in php. Now I need to print the content of a div for each record separately what I have now is the following:
<script type="text/javascript">
function printContent(div_id) {
var DocumentContainer = document.getElementById("<?php echo $route->flightnum;?>");
var html = '<html><head>' +
'</head><body style="background:#ffffff;">' + DocumentContainer.innerHTML +
'</body></html>';
var WindowObject = window.open("", "PrintWindow", "width=1000,height=1000,top=0,left=0,toolbars=no,scrollbars=yes,status=no,resizable=yes");
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
//WindowObject.close();
document.getElementById('print_link').style.display = 'block';
}
</script>
Then I have the following div:
<div id="brief_print" style="margin-left: 15px">
.
.
.
.
.
</div>
And perhaps a link like this:
<a class="glow" style="text-decoration: underline;" href="javascript:printContent('<?php echo $route->flightnum;?>')"id='print_link'>Open in new window for printing</a>
My problem is that when I hit the link I don't get anything in return. Please tell me where I'm wrong.
Thanks
The entire codes:
<?php
foreach($allroutes as $route)
{
?>
<tr>
<td onclick="$('#details_<?php echo $route->flightnum;?>').toggle('slow')">P</td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->depicao;?></td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->arricao;?></td>
<td style="padding:5px 10px" ALIGN="center"><?php echo $route->aircraft; ?></td>
<td style="padding:5px 10px"ALIGN="center"><?php echo $route->distance;?></td>
</tr>
<tr >
<td colspan="14"><div align="center">
<table cellspacing="0" width="100%" id="details_<?php echo $route->flightnum;?>" style="display:none">
<tr><th style="padding:5px 10px"><div align="center">Flight Details</div></th></tr>
<tr><td>
<section>
<?php
if(!isset($_GET['newwindow']))
{
?>
<a class="glow" style="text-decoration: underline;" href="javascript:printContent('<?php echo $route->flightnum;?>')"id='print_link'>Open in new window for printing</a>
<script type="text/javascript">
function printContent(div_id)
{
var DocumentContainer = document.getElementById("<?php echo $route->flightnum;?>");
var html = '<html><head>'+'</head><body style="background:#ffffff;">'+ DocumentContainer.innerHTML+ '</body></html>';
var WindowObject = window.open("", "PrintWindow","width=1000,height=1000,top=0,left=0,toolbars=no,scrollbars=yes,status=no,resizable=yes");
WindowObject.document.writeln(html);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
//WindowObject.close();
document.getElementById('print_link').style.display='block';
}
</script>
<?php
}
?>
<div id="brief_print" style="margin-left: 15px">
<pre>
<b>Departing</b> : <?php echo $schedule->depname ?>
<b>Arriving</b> : <?php echo $schedule->arrname ?>
<b>Equipment</b> : <?php echo $schedule->fullname;?>
<b>Alternate</b> : <?php echo $alternate1; ?>
<b>Alternate</b> : <?php echo $alternate2; ?>
<b>Alternate</b> : <?php echo $alternate3; ?>
</pre>
</div>
</section>
</td></tr>
</table>
</div>
<?php
}
?>

There are some big problems with your code. You don't seem to understand what is happening when and where. Let me try to explain.
When a client goes to a website, their browser sends a request for the page to your hosting server. Your host server executes the PHP code, producing the source code for the page to be sent to the client's browser. So the page is not sent back to the client's browser until after PHP has completely finished executing. No JavaScript code has executed at this point.
Once the page is sent to the browser (and PHP is done), that's when JavaScript takes over. JavaScript executes on the client-side.
So, once you understand this, you'll realize that you obviously cannot call PHP from JavaScript without making a new request to the server.
Try opening your page in a browser and view the source. You will see that you are defining the JS function printContent() once for every loop iteration. You are also missing closing tags for your second <tr> and <td> tags (add an extra </td></tr> after your last </div>). Perhaps you'll realize all the other problems when you view the source to see what your PHP code is producing.

Related

simple html dom grabbing all <p> attribute

I just learning about simple_html_dom.php, I try to get only all the p attribute content in entry-content class and make it to one paragraph or one sentence.
here the raw html file from the website that i want to get the content.
<div class="entry-content">
<p><img class="alignnone" src="xxxxxxxxxxx" width="800" height="450" /></p>
<p>data1<span id="more-287848"></span></p>
<p>data2</p>
<p>data3</p>
<p>data4</p>
<p>......</p>
<p>......</p>
<p>dataN</p>
<div class="wpa wpmrec">
<a class="wpa-about" href="https://wordpress.com/about-these-ads/" rel="nofollow"></a>
<div class="u">
<script type='text/javascript'>
(function(g){g.__ATA.initAd({sectionId:34789711, width:300, height:250});})(window);
</script>
</div>
</div>
</div>
here my code to get it :
<?php
require_once __DIR__.'/simple_html_dom.php';
$html = new simple_html_dom();
$html->load_file('https://xxxxxxxxx');
$isi = $html->find('div[class="entry-content"]',0)->innertext;
?>
<table border="1">
<thead>
<tr>
<td><?php echo $isi; ?></td>
</tr>
</thead>
</table>
how to do it? thank you guys.
You should be able to iterate all of the <p> elements and adding the text to a variable. I have not tried this, but something like this:
$complete = "";
foreach($html->find('div.entry-content p') as $p)
{
$complete .= $p->plaintext;
echo $p->plaintext;
}
echo $complete;
There's a lot of information in the documentation here:
http://simplehtmldom.sourceforge.net/manual.htm

html link tr broken not on place

I've tried making my hole tr tag to be clickable so I made this code
<?php foreach ($data['forums'] as $forum): ?>
<?php #var_dump($forum); ?>
<tr class="fix head">
<th class="fix ltext"><strong><?php echo $forum['name'] ?></strong></th>
<th class="fix rtext"><strong>Trending</strong></th>
<th class="fix ltext"><strong>Latest Post</strong></th>
</tr>
<?php foreach ($forum['children'] as $child): ?>
<?php #var_dump($child); ?>
<tr class="fix">
<a href="#">
<td class="fix ltext cl">
<strong><?php echo $child['name']; ?></strong>
<p><?php echo $child['description_html']; ?></p>
</td>
<td class="fix rtext cr">1423</td>
</a>
<td class="fix ltext cr cl">
tanya jawab sesuatu by <a class="u" href="#">=awdwad</a>
</td>
</tr>
<?php endforeach ?>
<?php endforeach ?>
on the view
The problem is
The link should be just above the highlighted part and below it. How could it be above far away, below my body tag?
Does ayone have any experience that could possibly generate this error?
Well, you've put the a tag where it's not allowed, so any browser's answer is legitimate. You should put an a into each td. Perhaps, you may alternatively handle click event for tr element, but that would require javascript.
The problem is that that is an invalid place for an <a>. No DTD will allow what you have tried to do.
Your approach is all wrong. You have to use Javascript to make a whole <tr> clickable. AFAIK, there is no way to do this in any HTML variant alone.
Try something like this:
<table>
<tr id="my_clickable_tr">
<td>Stuff</td>
<td>Stuff</td>
<td>Stuff</td>
</tr>
<!-- More table stuff -->
</table>
<script type="text/javascript">
document.getElementById('my_clickable_tr').onclick = function () {
window.location.href = 'http://wherever.you.want/to/send/the.clicker';
};
</script>
Anchor tags (a) are not allowed as children of table row (tr) tags, see the documentation here. Only table header (th) and table data (td) tags are allowed.

Problem with jquery load function

I have a page where I use the jquery load function to load data from a php file. The problem is that it does not always return the correct number of results. I have tried receiving the data straight from the php file and that worked perfectly. What could cause this problem with the load function?? It should only return a max of 5 entries.
s_showLoader();
$("#s_content").load("sdata.php", {s_page:1, user:fbusername}, s_hideLoader);
The show and hide loader functions just show and hide a gif loader. page is the page of pagination to display and user is just used to verify the user is logged in.
UPDATE 1:
Here is example of what the sample return looks like. It is in a while loop and is generated for each entry that is returned.
<table border="0">
<tr><td width="500">
<?php if($s_rows['name']){ echo $s_rows['name']; }else{ echo "User"; } ?><?php echo " | <a id='s_datedata".$s_idnum."'>".$s_rows['date']."</a>"; ?>
<div class="s_label<?php echo $s_idnum; ?>"><?php echo $s_display; ?></div>
<br>More Info...<br>
<div class="list_con" id="s_condata<?php echo $s_idnum; ?>"><?php echo $s_rows['content']; ?></div>
<div class="list_tags" id="s_contags<?php echo $s_idnum; ?>"><?php echo $s_rows['tags']; ?></div>
</td>
<td width="10" align="right"><!-- delete button if correct user is logged in-->
<?php
if($user && ($fbuser == $s_rows['fb_id']) ){
//show delete button
?>
<img src="close-icon.png" title="Click here to delete your listing" class="delopt" id="<?php echo $s_idnum; ?>" onclick="return del_list(<?php echo $s_rows['id']; ?>,<?php echo $s_rows['fb_id']; ?>)" height="25" width="25" alt="delete" border="0">
<?php
}else{
//echo $fbuser."|".$rows['fb_id'];
}
?>
</td>
</tr>
</table>
UPDATE 2:
This is the only POST variable in the php file. The rest of the data comes from the standard facebook api.
$s_page_num = floor($_POST['s_page']);
You can use html content instead of load.
function Load_Content() {
$.ajax({
url: 'yourfilename.php',
cache: false,
data: "s_page="+s_page_value,
success: function (html) {
$("#s_content").html(html);
}
});
}

make array object in for loop with horizontal view

<?php
$xml = simplexml_load_file('http://www.google.com/ig/api?weather=London');
$information = $xml->xpath("/xml_api_reply/weather/forecast_information");
$current = $xml->xpath("/xml_api_reply/weather/current_conditions");
$forecast_list = $xml->xpath("/xml_api_reply/weather/forecast_conditions");
?>
<html>
<head>
<title>Google Weather API</title>
</head>
<body>
<h1><?php print $information[0]->city['data']; ?></h1>
<h2>Today's weather</h2>
<div class="weather">
<img src="<?php echo 'http://www.google.com' . $current[0]->icon['data']?>" alt="weather"?>
<span class="condition">
<?php echo round(conver_f_c($current[0]->temp_f['data'])); ?>° C,
<?php echo $current[0]->condition['data'] ?>
</span>
</div>
<h2>Forecast</h2>
<?php foreach ($forecast_list as $forecast) : ?>
<div class="weather">
<img src="<?php echo 'http://www.google.com' . $forecast->icon['data']?>" alt="weather"?>
<div><?php echo $forecast->day_of_week['data']; ?></div>
<span class="condition">
<?php echo round(conver_f_c($forecast->low['data'])); ?>° C - <?php echo round(conver_f_c($forecast->high['data'])); ?>° C,
<?php echo $forecast->condition['data'] ?>
</span>
</div>
<?php endforeach ?>
</body>
</html>
<?php
function conver_f_c($F){
return $C = ($F − 32) * 5/9;
}
I want Out somthing like this manner of the horizontal ,
Even i tried UL LI WITH display inline but it goes failed,
Tell me some good suggestion for my probme,
I want exactly like horizontal, expecting exactly like screen shot ,
Tell me How to render using php
Thanks
alt text http://img163.imageshack.us/img163/7518/weatherhori.jpg
Above snippet present display out verticly , i want o change that verticle to horizonatal ,
somthing like this screen shot
<table>...</table>
Update
From your latest comment so far:
i know how to fetch array and display
it, but i dont know to fetch and
display in the verticl manner that is
the stuck up
I feel this is going to be a stupid answer but it appears to be what you don't understand...
The web is based in a markup language called HTML. This language has tags (delimited by angle-brackets) that allow you to define the structure of a document. On top of this, you have another language called CSS. This other lang allow you to define how HTML is going to be displayed on screen.
You may argue that you already have a web page and you've written it with the PHP language instead of the two other langs I've mentioned. That's not enterely true: you code in PHP, sure, but you use PHP to generate HTML. And it's HTML what finally reaches the browser (Firefox, Explorer...). PHP is executed in the web server, not in the browser. The browser can only see whatever HTML you've generated.
To sum up: you have to forget about PHP, Google and the whole weather thingy. You first need to write a static HTML document and style it with CSS. Once you've done with it, you can finally replace the parts of the information that are dynamic with values taken from your PHP variables.
And since you seem to need a table to display tabular data, the appropriate HTML tag is <table>:
<table>
<tr>
<th>Web</th>
<th>Thu</th>
<th>Fri</th>
<th>Sat</th>
</tr>
<tr>
<td><img src="/path/to/pics/cloudy.png" width="25" height="25" alt="Cloudy"></td>
<td><img src="/path/to/pics/sunny.png" width="25" height="25" alt="Sunny"></td>
<td><img src="/path/to/pics/rainy.png" width="25" height="25" alt="Rainy"></td>
<td><img src="/path/to/pics/cloudy.png" width="25" height="25" alt="Cloudy"></td>
</tr>
<tr>
<td>26ºC</td>
<td>26ºC</td>
<td>22ºC</td>
<td>25ºC</td>
</tr>
<table>
I suggest you find some tutorials about basic HTML and CSS. They'll be of invaluable help.
This is what's done by Google :
http://jsfiddle.net/bW8NA/1

basic php form help

I'm trying to make a calculator that will take inputs from users and estimate for them how much money they'll save if they use various different VoIP services.
I've set it up like this:
<form method="get" action="voip_calculator.php">
How much is your monthly phone bill?
<input name="monthlybill" type="text" value="$" size="8">
<p><input type="submit" name="Submit" value="Submit">
</p>
</form>
On voipcalculator.php, the page I point to, I want to call "monthlybill" but I can't figure out how to do it. I also can't figure out how to make it do the subtraction on the numbers in the rows.
This may be very simple to you but it's very frustrating to me and I am humbly asking for a bit of help. Thank you!
Here is the relevant stuff from voip_calculator, you can also click on the url and submit a number to see it in (in)action. I tried various times to call it with no success:
<table width="100%;" border="0" cellspacing="0" cellpadding="0"class="credit_table2" >
<tr class="credit_table2_brd">
<td class="credit_table2_brd_lbl" width="100px;">Services:</td>
<td class="credit_table2_brd_lbl" width="120px;">Our Ratings:</td>
<td class="credit_table2_brd_lbl" width="155px;">Your Annual Savings:</td>
</tr>
Your monthly bill was <?php echo 'monthlybill' ?>
<?php echo "$monthlybill"; ?>
<?php echo "monthlybill"; ?>
<?php echo '$monthlybill'; ?>
<?php echo 'monthlybill'; ?>
<?php
$monthybill="monthlybill";
$re=1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
while($offername!="") {
$offerlo ='offer'.$re.'logo';
$offerlogo=${$offerlo};
$offerli ='offer'.$re.'link';
$offerlink=${$offerli};
$offeran ='offer'.$re.'anchor';
$offeranchor=${$offeran};
$offerst ='offer'.$re.'star1';
$offerstar=${$offerst};
$offerbot='offer'.$re.'bottomline';
$offerbottomline=${$offerbot};
$offerca ='offer'.$re.'calcsavings';
$offercalcsavings=${$offerca};
echo '<tr >
<td >
<a href="'.$offerlink.'" target="blank">
<img src="http://www.nextadvisor.com'.$offerlogo.'" alt="'.$offername.'" />
</a>
</td>
<td >
<span class="rating_text">Rating:</span>
<span class="star_rating1">
<img src="IMAGE'.$offerstar.'" alt="" />
</span>
<br />
<div style="margin-top:5px; color:#0000FF;">
Go to Site
<span style="margin:0px 7px 0px 7px;">|</span>
Review
</div>
</td>
<td >'.$offercalcsavings.'</td>
</tr>';
$re=$re+1;
$offer ='offer'.$re.'name';
$offername= ${$offer};
}
?>
offercal(1,2,3,4,5,6,7)savings calls to a file called values.php where they are defined like this:
$offer1calcsavings="24.99";
$offer2calcsavings="20.00";
$offer3calcsavings="21.95";
$offer4calcsavings="23.95";
$offer5calcsavings="19.95";
$offer6calcsavings="23.97";
$offer7calcsavings="24.99";
One thing i do is this
echo "<pre>";
print_r($_GET);
echo "</pre>";
Put this somewhere in your receiving end and you'll get an understanding of whats happening.
Not enough detail provided for an answer but let's simplify and assume you have the 'savings' numbers in an array, say, companySavings . So, you need to subtract each of these from the value the user specifies right? You don't need to call something (you could if you want...)
when the user clicks 'Submit' and the page is loaded again pull the monthlybill into a var e.g.
$monthlyBill = $_GET['monthlybill']; //you should do some checking to prevent attacks but that's another matter
Then, when you are building the list of savings it would look something like this
<?php
//...code for the rest of the page and starting your table
foreach($companySavings as $savings){//insert each row into the table
echo("<tr><td>".(comapnyName/Image whatever..)."</td><td>$".$monthlyBill-$savings."</td></tr>);
}
//... end the table and rest of code
?>
You need to get the value from the QueryString and put it into a PHP variable.
Like this:
$monthlyBill = $_GET['monthlybill'];
Now the variable $monthlyBill contains the value from the QueryString.
To display it:
echo "Your monthly bill is: $monthlyBill";

Categories