Issue including page.php on page - php

I am trying to include a vbijreizen_kaart.php within some code. When I put it outside of the echo, but within the loop (i think this is correct terminology). It will load my google map, however, it will not bring back the rest of the results in my (while) results. When I remove it, the results appear, so I'm doing something wrong but I just don't know what.
The bit of code that is where I am having problems is:
$result = mysql_query($sql);
echo "<br />"; ?>
<?php include('vbijreizen_kaart.php');?>
<?php
echo "<table border='0' cellspacing='0'>
<tr>
<th width='20' valign='top'><div align='left'> </div></th>
</tr>
<tr>
<th height='1' colspan='9' valign='top' bgcolor='#333333'></th>
</tr>";
while($row = mysql_fetch_array($result)){
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<td bgcolor='$row_color'>" .date("d-M-Y H:i",
strtotime($row['vertrekdatum2'])). "</td>";
echo "<td bgcolor='$row_color'><img src='../flags/".$row['countryflag']."'>
<abbr title=\"".htmlspecialchars($row['luchthavennaam'])."\">
".$row['luchthavencode']."</abbr></td>";
echo "<td bgcolor='$row_color'><img src='../flags/".$row['countryflagaankomst']."'>
<abbr title=\"".htmlspecialchars($row['aankomstnaam'])."\">
".$row['aankomstluchthaven']."</abbr></td>";
I have tried doing just
echo "<br />";
echo include('vbijreizen_kaart.php');
echo " blah blah blah
and I've tried as above, and nothing will let me display the map that exits on vbijreizen_kaart.php as well as the data that would display in the while results.
I can verify in Toad that I have the Query working as it should.

include is not a method, so syntax can look like: include 'vbijreizen_kaart.php';, but that's not the problem. include basically allows you to access the classes, functions, and variables that are on the included page. You don't echo or print the included page itself, but rather the classes, functions, and any variables on the included page. Also, the included page can echo or print to the page you include it into, so it could be a matter of position.

Related

Button not being called with isset() function

I'm having some trouble trying to build a very simple inventory management system.
What I'm doing is showing the data from a database in a html table and in each row a create two buttons: one to edit and one to delete the item. The problem is that I'm not being able to call these buttons with the isset() function and I can't understand why. I've tried to create a specific function for these but still doesn't work. Anybody has any idea?
Here is the code:
P.S.: Don't mind small english erros or a lack of of brackets. I had to change the code a little bit.
function searchTablet(){
if(isset($_POST['btnSearchTablet'])){
global $connection;
$query="SELECT * FROM tablet";
$run=mysqli_query($connection, $query);
echo "<table class='table table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Brand</th>";
echo "<th>Model</th>";
echo "<th>Color</th>";
echo "<th>Price</th>";
echo "<th>Fabrication Date</th>";
echo "<th>Provider</th>";
echo "<th>Registration Date</th>";
echo "<th>Edit</th>";
echo "<th>Delete</th>";
echo "</tr>";
echo "</thead>";
while($obj=mysqli_fetch_object($run)){
echo "<tr>";
echo "<td>$obj->id</td>";
echo "<td>$obj->idBrand</td>";
echo "<td>$obj->idModel</td>";
echo "<td>$obj->idColor</td>";
echo "<td>$obj->price</td>";
echo "<td>$obj->fabricationDate</td>";
echo "<td>$obj->idProvider</td>";
echo "<td>$obj->registrationDate</td>";
echo "<td><a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a></td>";
echo "<td><a href='resultTablet.php?btnDeleteTablet{$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a></td>";
echo "</tr>";
if(isset($_POST["btnDeleteTablet{$obj->id}"])){
$idTablet=$obj->id;
$delQuery="DELETE FROM tablet WHERE id='$idTablet'";
$delRun=mysqli_query($connection, $delQuery);
if($delRun){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
}
As always... looks like StackOverflow users are toxic and unfriendly and don't even try to help new people who want to get into programming.
So I will try to help you a little bit.
If you are passing parameters via URL you have to use $_GET not $_POST method.
Also, I would change
<a href='resultTablet.php?btnEditTablet{$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
to
<a href='resultTablet.php?btnEditTablet={$obj->id}'class='btn btn-primary' name='btnEditTablet'>Alterar</a>
So you could do this:
if(isset($_GET["btnDeleteTablet"])){
and you would get id via $_GET like this:
$idTablet=$_GET["btnDeleteTablet"];
After this change, you can close while loop you used before
this if(isset($_GET["btnDeleteTablet"])) line, also you wont need $obj->id
anymore, because you will $_GET data decoupled from while loop or any other code you wrote before.
On another note, I see you forgot <tbody> </tbody> in your table.
EDIT:
Also, what are you doing with
if(isset($_POST['btnSearchTablet'])){
This wont work after delete button click.
You shouldn't use it like that, because after the delete button click your page will go to URL with $_GET parameters and that if logic will prevent
if(isset($_GET["btnDeleteTablet"])){
logic working.
So move whole
if(isset($_GET["btnDeleteTablet"])){
...
}
out of if(isset($_POST['btnSearchTablet'])){
Read up about $_POST and $_GET methods also, read about forms
you really need it.
Also, I recommend you to get program to profile requests so you could see how post and get data moves.
I recommend you to install Fiddler program, so you would be able to see how post, get data moves.
Ok, I will try to fix your code at last so it could work:
<?php
function searchTablet(){
global $connection;
if(isset($_GET['btnDeleteTablet'])){
deleteTablet();
}
//I dont why are you using it so I commented it out.
//if(isset($_POST['btnSearchTablet'])){
//}
displaySearchTablet();
}
function displaySearchTablet(){
global $connection;
$query = "SELECT * FROM tablet";
$run = mysqli_query($connection, $query);
while($obj = mysqli_fetch_object($run)){
//Combine all rows into one variable
$table_rows .= "
<tr>
<td>{$obj->id}</td>
<td>{$obj->idBrand}</td>
<td>{$obj->idModel}</td>
<td>{$obj->idColor}</td>
<td>{$obj->price}</td>
<td>{$obj->fabricationDate}</td>
<td>{$obj->idProvider}</td>
<td>{$obj->registrationDate}</td>
<td>
<a href='resultTablet.php?btnEditTablet={$obj->id}' class='btn btn-primary' name='btnEditTablet'>Alterar</a>
</td>
<td>
<a href='resultTablet.php?btnDeleteTablet={$obj->id}' class='btn btn-danger' name='btnDeleteTablet'>Excluir</a>
</td>
</tr>";
}
$result_table =
"<table class='table table-striped'>
<thead>
<tr>
<th>ID</th>
<th>Brand</th>
<th>Model</th>
<th>Color</th>
<th>Price</th>
<th>Fabrication Date</th>
<th>Provider</th>
<th>Registration Date</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
$table_rows
</tbody>
</table>";
echo $result_table;
}
function deleteTablet(){
global $connection;
$id = $_GET['btnDeleteTablet'];
$query = "DELETE FROM tablet WHERE id = '$id'";
$run = mysqli_query($connection, $query);
if($run){
echo "<div class='alert alert-success' role='alert'>Device was successfuly deleted.</div>";
}else{
echo "<div class='alert alert-danger' role='alert'>Error.</div>";
}
}
I kept it very basic, so you would be able to understand.
I didn't pass parameters or returned anything so it would be more understandable to you.
Good luck on your journey to programming.
isset() will return false if an empty string is being submitted, so try using !empty() instead.

fetch tags inside php

I am trying to get some div tags inside my php. The original code that is working looks like this:
echo "Date: ".$row{'date'}."<br>"."Day: ".$row{'day'}."<br>"."From Time: ".$row{'fromtime'}."<br>"."To Time: ".$row{'totime'}; //display the results
When I run this code, the data is gonna be below each other. I would like that each output from the database, is coming out in a row and in some div tags, so I can control it. I have tried to make it, but I am really in doubt how the syntax are?
<?php
echo "<div class="column1">""Date: ".$row{'date'}"</div>";
echo "<div class="column2">""Day: ".$row{'day'}"</div>";
echo "<div class="column3">""From Time: ".$row{'fromtime'}"</div>";
echo "<div class="column4">""To Time": ".$row{'totime'}"</div>";
?>
This dosent work. Does anybody know how I can solve this?
New code:
Thanks a lot for both your answers. I am actually thinking of putting in a table instead. When I print out the div, it looks like this:
http://postimg.org/image/jq60jujv1/
So I guess it would be something with
//fetch the data from the database
Print"<h3>Return from database:</h3>"."<br>";
print '<table border="1">';
print '<tr>';
print '<td>date</td>';
print '<td>day</td>';
print '<td>fromtime</td>';
print '<td>totime</td>';
print '</tr>'."<br>";
print '</table>'."<br>";
while ($row = mysql_fetch_array($result)) {
print "<tr>"."<td>"."Date: ".$row{'date'}."</td>"."</tr>";
print "<tr>"."<td>"."Day: ".$row{'day'}."</td>"."</tr>";
print "<td>"."From Time: ".$row{'fromtime'}."</td>";
print "<td>"."To Time: ".$row{'totime'}."</td>"."<br>";
}
or?
Best Regards
Mads
try this instead of yours,
<?php
echo "<div class='column1'>"."Date: ".$row{'date'}."</div>";
echo "<div class='column2'>"."Day: ".$row{'day'}."</div>";
echo "<div class='column3'>"."From Time: ".$row{'fromtime'}."</div>";
echo "<div class='column4'>"."To Time: ".$row{'totime'}."</div>";
?>
Because of the wrapping quotation marks you use for echo ".." you can not use the same quotation marks inside that string:
echo "<div class="column1">""Date: ".$row{'date'}"</div>";
^--- string ended
You can solve this in several ways, using single quotation marks inside is already suggested, but you can also escape the ones you are currently using:
echo "<div class=\"column1\">Date: ".$row{'date'}."</div>";
^ ^
You can avoid all these problems by not using echo for output of all markup, but rather start/end php tags the place you actually want to print
variables or do some logic:
<div class="column1">Date: <?php echo $row{'date'}; ?></div>
Output as table
A HTML table use the <table> and </table> tags,
<tr> is a table row, <th> for table
headers and <td> for regular table cells.
<?php
/* the code block where you connect to db etc.. */
?>
<table>
<!-- headers -->
<tr>
<th>Date</th>
<th>Day</th>
<th>From</th>
<th>To</th>
</tr>
<!-- Now a row for each new set of data, here you probably need to
loop through some data set you retrieve from the database -->
<?php while($row = mysql_fetch_array($result)): ?>
<tr>
<td><?php echo $row{'date'};?></td>
<td><?php echo $row{'day'};?></td>
<td><?php echo $row{'fromtime'};?></td>
<td><?php echo $row{'totime'};?></td>
</tr>
<?php endwhile; ?>
</table>
Try this
<?php
echo "<div class='column1'>Date: ".$row{'date'}."</div>
<div class='column2'>Day: ".$row{'day'}."</div>
<div class='column3'>From Time: ".$row{'fromtime'}."</div>
<div class='column4'>To Time: ".$row{'totime'}."</div>";
?>
Edit:
If you want output as table
<table>
<tr>
<th>Date</th>
<th>Day</th>
<th>From Time</th>
<th>To Time</th>
</tr>
<?php
foreach($result as $row){
echo "<tr><td>".$row{'date'}."</td>
<td>".$row{'day'}."</td>
<td>".$row{'fromtime'}."</td>
<td>".$row{'totime'}."</td></tr>";
}
?>
</table>

How to echo html and row from database

I have a script written to grab a row from my database based on current session user, which outputs the row correctly, however I want to insert a small image to be displayed alongside of the echo'd row, and cannot figure out the proper syntax.
if ($row['lifetime']!="")
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div> ".$row['lifetime'];
else
echo '';
?>
basically I want the image to appear right before or after the .$row appears, either or.
You can try:
<?php
if ($row['lifetime'] !== "") {
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div>";
echo $row['lifetime'];
echo "<img src='' alt='' style='width:100px'/>";
}
?>
Just put the HTML for the image into the string you're echoing:
echo "<div style ='font:12px Arial;color:#2F6054'><img src="fill in URL here"> Lifetime Member: </div> ".$row['lifetime'];
You can try as below example
HTML
<table>
<thead>
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Photo</th>
<th ></th>
</tr>
</thead>
<tbody>
<?php
$tst=0;
$result = mysql_query("select * from acc_cust");
while($row = mysql_fetch_array($result))
{
echo "<tr class='odd gradeX'>";
echo "<td width=5%'>" . $row['ent_no']. "</td>";
echo "<td>" . $row['cust_name']. "</td>";
echo "<td><img src='[path]" . $row['cust_img'] . "' /></td>";
}
?>
</tbody>
</table>

Use output from MySQL to extract additional info

How can I use the output of a MySQL query as to extract extra info from the database about that output?
For example, if my query generates a list of names and there is extra info about each name existing in the database, when I click on the name on the output page, the system will extract the relevant info from the database and show it on another page.
EDIT: Here's the code:
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
?>
<tr align="center">
<td width="5%">
<a href="// I don't know what do put here " >
<?php print("{$row["pid"]}");?>
</a>
</td>
<td><?php print("{$row["place"]}");?></td>
<td><?php print("{$row["date"]}");?></td>
<td><?php print("{$row["ppp"]}");?></td>
<td><input type="button" value="Book"></td>
</tr> <?php } ?>
Set each row of the first output results equal to a variable, and then have an if statement in php that looks for strings in those variables, and then (upon some sort of a page refresh button since php is server-side code) displays the additional content.
Great; here's what you're missing:
<a href="<?php // I don't know what to put here ?>" >
Should be:
<a href="<?php echo 'database.php?action=viewrecord({$row["pid"]})'?>" >
From there, you should be able to extract the relevant info using the pid of the user.
On a side note, unless you're really worried about spacing, you should just dump out the relevant HTML; doing so will save you the task of repeatedly typing <?php ?> every time you want to dump out a variable.
<?php
// you can delete mysql-assoc
while($row = mysql_fetch_array($rs, MYSQL_ASSOC)) {
echo "<tr align='center'>";
echo "<td width='5%'>";
echo "<a href='database.php?action=viewrecord({$row['pid']})'>";
echo "{$row['pid']}";
echo "</a>";
echo "</td>";
echo "<td>{$row['place']}</td>";
echo "<td>{$row['date']}</td>";
echo "<td>{$row['ppp']}</td>";
echo "<td><input type='button' value='Book'></td>";
echo "</tr>";
}
?>
Thanks for the help but this doesn't work :( I'm beginner in php when I press the record the url looks like localhost/ass/database.php?action=viewrecord%28{$row[
This isnt a direct answer to your question perse but since youre new to php im goign to through it out there. The syntax you uisng makes unicorns cry. It give me a headache jsut tryign to decipher it. Heres the same code in an alternative way:
<?php while($row = mysql_fetch_array($rs, MYSQL_ASSOC)): ?>
<tr align="center">
<td><?php printf('%s', url_encode('viewrecord('.$row['pid'].')'), $row['pid']); ?></td>
<td><?php echo $row['place']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['ppp']; ?></td>
<td><input type="button" value="Book"></td>
</tr>
<?php endwhile; ?>
However this database.php?action=viewrecord() worries me. I hope youre not using eval to execute a function passed by URL. Typically this would look something more like: database.php?action=viewrecord&id=2. You would verify that the action parameter is valid, and then invoke a function tied to that parameter which may or may not be the actual function name, and you would pass in the id parameter.
Eaxample database.php:
$action = isset($_POST['action']) ? $_POST['action'] : 'index';
switch($action) {
case 'viewrecord':
if(isset($_POST['id']) {
viewrecord((integer) $_POST['id']);
}
break;
// a whole bunch of other cases here for other actions
default:
// the default thing to do if an action is in valid
}
// the rest of your processing

Jquery alert printing outhtml tags

I have PHP code that creates a table with values from my DB. But when i try to Alert the table in JQuery it displays the html tags aswell as the correct values.
Can someone tell me how to stop the tags from being displayed.
Hers the code for the table
echo "<table border='1' width='150' cellpadding='0' cellspacing='0'>";
echo"<tr><th>Stats</th><th>name</th></tr> ";
echo "<tr><td>var</td> <td>{$var} </td></tr>";
echo "<tr><td>var1</td> <td> {$var1} </td></tr>";
echo "<tr><td>var2</td> <td> {$var2}</td> </tr>";
echo "<tr><td>var3</td> <td>{$var3} </td></tr>";
echo "<tr><td>var4 </td> <td>{$var4 }</td></tr>";
echo "<tr><td>var5</td> <td>{$var5} </td></tr>";
echo "<tr><td>var6</td> <td>{$var6} </td></tr>";
echo "</table>"
thanks, heres the jquery
$('.but').live('click', function()
{
$.post('display.php',function(output)
{
alert(output);
});
});
THis is what is getting shown in the alert
<table border='1' width='150' cellpadding='0' cellspacing='0'><tr><th>Stats</th>
<th>name</th></tr> <tr><td>name</td> <td>0 </td></tr><tr><td>var1</td> <td> 2 </td>
</tr><tr><td>var2</td> <td> 90</td> </tr><tr><td>var3 </td> <td>0 </td></tr><tr>
<td>var 4 </td> <td>2</td></tr><tr><td>var5</td> <td>0 </td></tr><tr><td>var6</td>
<td>0 </td></tr></table>
I want table shown in the alert , not the above ,
when you alert something it tries to covert the value toString thats why the output is not rendered as HTML table as it is in the browser. If you want to display the out put use some modal plugin like the jquery UI modal.
DEMO
If you want to display html in an alert as text, you would need to parse the text out of each cell and format it into a text string using "\n" as line breaks.
A simpler solution would be to use one of the many alert plugins that allow use of either html or text with line breaks such as :
http://labs.abeautifulsite.net/archived/jquery-alerts/demo/
Or return formatted text from server if all you want is a broser default alert.

Categories