PDO Messed Up My CSS / HTML Layout - php

One of my webpages that had perfect format under MySQL_* functions, suddenly got mixed up when I translated the old functions to PDO. The problem is that my Copyright bar is now floating above my table of MySQL data, while it should be at the bottom of the page. I'm not sure why it's happening. Any help is appreciated!
This is the portion of the file that I believe may be the problem:
<div id="content" style="float:center;background-image:url('http://tinyurl.com/bds4u2j');">
<ul class="menu">
<li>
<?php
if(!isset($_COOKIE['username'])){
echo "Welcome, guest!";
}else{
echo "Welcome, " . $_COOKIE['username'];
}
?>
</li>
<li><br></li>
<li><a class="item" href="index.html">Home</a></li>
<li><a class="item" href="classes.html">Classes</a></li>
<li><a class="item" href="contact.html">Contact</a></li>
<?php
if(!isset($_COOKIE['username'])){
echo '<li><a class="item" href="login.html">Login</a></li>';
}else{
echo '<li><a class="item" href="upload.html">Upload</a></li>';
echo '<li><a class="item" href="password_protect.php?logout=1">Logout</a></li>';
}
?>
</ul>
<center><h1>Mr. Stanford's first period</h1></center>
<br>
<center><h3>Your class files are available for download below</h3></center>
<br>
<br>
<center>
<?php
echo "<table border='1'>
<tr>
<th>Filename</th>
<th>Description</th>
<th>Download</th>";
IF(isset($_COOKIE["username"]))
{
echo "<th>Delete</th>","</tr>";
}
ELSE
{
echo "</tr>";
}
try{
$dbh = new PDO('mysql:host=$host;port=$port;dbname=$dbname','$username','$password');
foreach($dbh->query('SELECT * from period1') as $row) {
echo "<tr>";
echo "<td>" . $row['Filename'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td><a href = ". $row['File'] ." ><button>Download</button></a></td>";
IF(isset($_COOKIE["username"]))
{
echo "<td><a href = 'deletefile.php?$s_id=". $row['s_id'] ."'><button>Delete</button></a></td>","</tr>";
}
ELSE
{
echo "</tr>";
}
echo "</table>";
}
$dbh=null;
} catch (PDOException $e) {
print "<br>ERROR: " . $e->getMessage() . "<br/>";
die();
}
$dbh=null;
?>
</center>
<br>
<br>
<br>
</div>
<div id="footer" style="background-color:#99FF33;clear:both;text-align:center;">
Copyright © David Schilpp 2013, All Rights Reserved
</div>
Again, thank you to anybody who can help me debug this problem!

I guessed that your table was not closed. see comments on question.
When you are facing this kind of problem (broken html), you should first analyse the html generated.
You can right-click on page and select View page source.
You can use tools freely available too.
As always, StackOverflow is great place to find answer and new stuff

Related

Same page is loading when clicking pagination in DataTable PHP

When I click page number in DataTable, it is not moving to next page instead it loads the whole page again.
But if I click the DataTable sorting button and again I click the pagination page number, the DataTable is working fine. Can anyone help me to solve this. Thanks in advance.
<?php include 'header.php'; ?>
</style>
<!-- Breadcrumbs -->
<section class="bg-gray-7">
<div class="breadcrumbs-custom box-transform-wrap context-dark">
<div class="container">
<h3 class="breadcrumbs-custom-title">Sailing Schedules</h3>
<div class="breadcrumbs-custom-decor"></div>
</div>
<div class="box-transform" style="background-image: url(images/bg-typography.jpg);"></div>
</div>
<div class="container">
<ul class="breadcrumbs-custom-path">
<li>Home</li>
<li class="active">Sailing Schedule</li>
</ul>
</div>
</section>
<br>
<br>
<div class="container">
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Include config file
require 'config.php';
$sql = "SELECT * FROM long_sched";
if($result = mysqli_query($con, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table-custom table-custom-primary' id ='long_schd_table'>";
echo "<thead>";
echo "<tr>";
echo "<th>Service Name</th>";
echo "<th>Description</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><a href=http://localhost/permatest/". $row['long_filepath'] . " target='_self' download>".$row['long_service']."</a></td>";
echo "<td>" . $row['long_desc'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
// Free result set
mysqli_free_result($result);
} else {
echo "<p class='lead'><em>No records were found.</em></p>";
} else {
echo "ERROR: Could not able to execute ".$sql." " . mysqli_error($con);
}
// Close connection
mysqli_close($con);
?>
</div>
</section>
<br>
<br>
<?php include 'off_footer.php'; ?>
<script>
$('#long_schd_table').DataTable({});
</script>
The above code is giving this error but in my console I didn't get any error information.

HTML inside PHP not working

I have been trying to put HTML inside PHP for a while, but I could never get it to work. Instead I have had to use this
<?php //code in here
while($record = mysql_fetch_array($myData)){
?>
//HTML code
<?php
}
?>
and it has worked for a while, but now I'm implementing a search function to the page, and I need all HTML to be dynamically generated, or the page will be displayed twice. Therefore, i'm in need of some help. This is the old-new code I have at the moment.
<?php
$sql = "SELECT * FROM `LISTINGS` ORDER BY YA DESC $limit";
$myData = mysql_query($sql,$con);
while($record = mysql_fetch_array($myData)){
//this is what i have manage to get done
echo '<div class="cards-row">';
echo '<div class="card-row">';
echo '<div class="card-row-inner">';
echo '<div class="card-row-image" data-background-image="http://domains/$record["IMAGENAME"].jpg">';
echo '</div>';
echo '<div class="card-row-body">';
echo '<h2 class="card-row-title">';
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
echo '</a>';
echo '</h2>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
?>
Static/dynamic code
<div class="card-row">
<div class="card-row-inner">
<div class="card-row-image" data-background-image="http://www.domain/<?php echo $record['IMAGENAME'];?>.jpg" width="150" height="180" ">
</div><!-- /.card-row-image -->
<div class="card-row-body">
<h2 class="card-row-title">
<a href="http://www.domain/listing-detail.php?ID=<?php echo $record['ID'];?>"> <?php custom_echo ($record['TITLE'], 80); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?>
</a>
</h2>
<div class="card-row-content"><?php custom_echo ($record['DESCRIPTION'], 250); ?><?php if($record['STREET']===' '){ echo "Not provided" ;}?></div><!-- /.card-row-content -->
</div><!-- /.card-row-body -->
<div class="card-row-properties">
<dl>
<dd></dd><dt>Visit Website</dt>
<dd></dd><dt>More Info</dt>
<dd>Added</dd><dt><?php echo $record['YA'];?>/<?php echo $record['MU'];?>/<?php echo $record['DU'];?></dt>
<dd>Viewed</dd><dt>Visited</dt>
</dl>
</div><!-- /.card-row-properties -->
</div><!-- /.card-row-inner -->
</div><!-- /.card-row -->
</div><!-- /.cards-row -->
<br/>
<?php
}
?>
My goal is to put all the code above inside
while($record = mysql_fetch_array($myData)){
Although this is not the best way when it comes to readability and maintainability, the way I usually process HTML with PHP is by echoing it.
echo "<div style=\"color:red\">test</div>";
If I need to add PHP content then
$myVar = "test";
echo "<div style=\"color:red\">" . $myVar . "</div>";
And if I already have some HTML code I'd like to escape, I can easily do so using Notepad++'s replace all function by replacing " with \"
You have an error in line 15
echo '<a href="http://domain/listing-detail.php?ID='; $record['ID']echo'">'
Change it to:
echo '<a href="http://domain/listing-detail.php?ID='.$record['ID'].'">';
Also, are you saving it in ".php" extension?.
After the last echo, you forget a '}'.
Finally, remove your mysql_ function and use mysqli_ or PDO, if you're on PHP7.

Tabs to navigate page by page

I use bootstrap with JQuery and I would like to use tabs to navigate page by page.
Indeed, I have some results in a table. With SQL, I know how to do this by using LIMITE 1,10 for example. And by using a $_GET argument to navigate page by page...
But here, I would like use tabs instead.
Here is my code:
echo '<div class="tab-content">';
echo '<div class="tab-pane fade in active" id=1>';
while($row = $selectAllUsersM->fetch(PDO::FETCH_OBJ)){
$isActive = ($row->Activation) ? 'Desable' : 'Enable';
echo "<tr>"; echo '<td>'.$row->Firstname.'</td>';
echo '<td>'.$row->Name.'</td>';
echo '<td>'.$row->Nickname.'</td>';
echo '<td>'.$row->Email.'</td>';
echo '<td>'.timeToDDMMYYYY($row->DateInscription).'</td>';
echo '<td><a href="?param='.$row->Activation.'&id='.$row->IdUser.'" title='.$isActive.'>'.isActive($row->Activation).'</a></td>';
echo "</tr>";
}
echo '</div>';
echo '</div>';
Here is the example for pagination:
<ul class="pagination">
<li>«</li>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>»</li>
</ul>
I don't know how can I display 10 results on each page. Then, if 11 results, create a new page and a new tab. I hope you know what I mean.
Thank you for your help ;)
Well, I solved this by using a POST in Ajax.
Function to display results
<script>
function displayPilotesF(pageF) {
$.post("pilotesF.php",
{ page : pageF},
function(data){
//alert("Data Loaded: " + data);
$("#pilotesF").html(data);
}
);
}
</script>
I display the results in a div:
<div id="pilotesF"></div>
And on my page "pilotesF.php":
if(isset($_POST['page'])){
$page = $_POST['page'];
$selectAllUsersF= getAllInfoOfAllUsersF($page, nbPiloteFPerPage);
$selectAllUsersF->execute();
echo "<table class='table table-hover table-condensed'>";
echo "<th>Firstname</th>";
echo "<th>Name</th>";
echo "<th>Nickname</th>";
echo "<th>Email</th>";
echo "<th>Registration</th>";
echo "<th>State account</th>";
while($row = $selectAllUsersF->fetch(PDO::FETCH_OBJ)){
$isActive = ($row->Activation) ? 'Disable' : 'Enable';
echo "<tr>";
echo '<td>'.$row->Firstname.'</td>';
echo '<td>'.$row->Name.'</td>';
echo '<td>'.$row->Nickname.'</td>';
echo '<td>'.$row->Email.'</td>';
echo '<td>'.timeToDDMMYYYY($row->DateInscription).'</td>';
echo '<td><a href="?param='.$row->Activation.'&id='.$row->IdUser.'" title='.$isActive.'>'.isActive($row->Activation).'</a></td>';
echo "</tr>";
}
echo "</table>";
}
And for my pagination:
$nbPages= ceil($nbPiloteF/ nbPiloteFPerPage);
echo '<div class="text-center">';
echo '<ul class="pagination" id="tabs2">';
/***********************************/
for ($i = 1 ; $i <= $nbPages; $i++){
if($i == 1)
echo '<li class="active"><a data-toggle="tab" OnClick="displayPilotesF('.$i.');">'.$i.'</a></li>';
else
echo '<li><a data-toggle="tab" OnClick="displayPilotesF('.$i.');">'.$i.'</a></li>';
}
/***********************************/
echo '</ul>';
echo '</div>';
Maybe it could help some of you guys,
Thank you anyway, have a night! :)

How to Detect a Button Click in an Expanded Accordion Row with Multiple Labels

This is an extension to another post by myself (How To Detect a Button Click in the Expanded Accordion Row).
After adding some additional rows in the details section of my accordion control and moving the 'Select' button into a separate div, I am only getting the values from the first accordion row. This was my original issue. Just to confirm the original solution worked, if I move my 'Select' button into the save div containing the label for Location, it passes the correct location label ('labelAccordionLocation') value for what ever row I have expanded but always passes the first row's name label value. It also works correct if the 'Select' button is in the save div as the Name label ('labelAccordionName').
Based on the suggestion in my other post (which worked flawlessly for my original example), it seems that I now need to figure out how to identify the div that contains each label so I can then pass it's value when the 'Submit' button is clicked. Although, this is still just an amateur's way of thinking.
<div class="accordion" id="accordion2">
<?php
// SQL stuff here...
$group = null;
while ($row = odbc_fetch_array($db)) {
if ($row['Name'] != $group) {
echo "<div class='accordion-heading'>";
echo "<a class='accordion-toggle' data-toggle='collapse'
data-parent='#accordion2' href='#", $row['Number'],"'>
<button id='buttonAccordionToggle'
data-target='#", $row['Number'],"'>", $row['Name'],"</button></a>";
echo "</div>";
echo "<div id='", $row['Number'],"' class='accordion-body collapse'>";
echo "<div class='accordion-inner'>";
echo "<div class='control-group' style='min-height: 50px'>";
echo "<div class='row-fluid show-grid'>";
echo "<div class='span12'>";
echo "<div class='span1'>";
echo "<label>Name:</label>";
echo "</div>";
echo "<div class='span11'>";
echo "<label id='labelAccordionName'>",
$row['Name'],"</label>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='row-fluid show-grid'>";
echo "<div class='span12'>";
echo "<div class='span1'>";
echo "<label>Location:</label>";
echo "</div>";
echo "<div class='span11'>";
echo "<label id='labelAccordionLocation'>",
$row['Location'],"</label>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='row-fluid show-grid'>";
echo "<div class='span12'>";
echo "<a id='select' class='btn'>Select</a>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
$group = $row['Name'];
}
}
}
?>
</div>
<div id="divDetails" style="display: none;">
<label>Name:</label>
<label id="labelName"></label><br />
<label>Location:</label>
<label id="labelLocation"></label><br />
<a id="close" class="btn">Close</a>
</div>
<script>
// Hide the accordion and show the hidden div
$('a.btn').click(function() {
$('#accordion2').hide();
$('#divDetails').show();
// Pass the label value from the accordion row to the hidden div label
$('#labelName').html($('.accordion-body.in .labelAccordionName').html());
$('#labelLocation').html($('.accordion-body.in .labelAccordionLocation').html());
});
// Hide the div and show the accordion again
$('#close').click(function() {
$('#accordion2').show();
$('#divDetails').hide();
});
// Only allows one row to be shown at a time
$('.accordion-toggle').click(function() {
var $acc = $('#accordion2');
$acc.on('show', '.collapse', function() {
$acc.find('.collapse.in').collapse('hide');
});
});
$(document).ready(function(){
// On dropdown change, pass in value to populate accordion/details
$('#dd').change(function() {
var r = $(this).val();
location.href = "test.php?src=" + r;
});
});
</script>
I did some searching on the jQuery site to try and figure this one out but unfortunately was unable to get it working. I'll do my best to give any additional information that is needed.
Edits
I changed the label's to use classes instead of id's. Although a great suggestion, I did not have to apply a new class 'activeAccordion'. Since the class 'in' is applied to the expanded row, I was able to use it for access as seen below. I also updated the jsFiddle and provided a link at the bottom.
$('a.btn').click(function() {
$('#accordion2').hide();
$('#divDetails').show();
$('#labelName').html($('.accordion-body.in .labelAccordionName').html());
$('#labelLocation').html($('.accordion-body.in .labelAccordionLocation').html());
});
Working jsFiddle
http://jsfiddle.net/N8JuQ/
You should consider escaping out of PHP in your if statement and only jump back in to echo the variables. Also read up on alternative syntax: http://php.net/manual/en/control-structures.alternative-syntax.php
while ($row = odbc_fetch_array($db)) {
if ($row['Name'] != $group){
?>
<div class='accordion-heading'>
<a class='accordion-toggle' data-toggle='collapse' data-parent='#accordion2' href='#", <?php echo $row['Number']; ?>,"'>
<button id='buttonAccordionToggle' data-target='#", <?php echo $row['Number']; ?>,"'>", <?php echo $row['Name']; ?>,"</button></a>
</div>
<div id='", <?php echo $row['Number']; ?>,"' class='accordion-body collapse'>
<div class='accordion-inner'>
...
...
...
<?php
}
?>
when generating html with php, as long as you are iterating be careful with IDs. Never generate same ID in a DOM, it messes things really bad. (ie. select, labelAccordionName, buttonAccordionToggle) I suggest using classes or generating unique IDs plus a generic class
you should have something like :
<div class="accordion" id="accordion2">
<?php
// SQL stuff here...
$group = null;
while ($row = odbc_fetch_array($db)) {
if ($row['Name'] != $group) {
echo "<div class='accordion-heading'>";
echo "<a class='accordion-toggle' data-toggle='collapse'
data-parent='#accordion2' href='#", $row['Number'],"'>
<button class='buttonAccordionToggle'
data-target='#", $row['Number'],"'>", $row['Name'],"</button></a>";
echo "</div>";
echo "<div id='", $row['Number'],"' class='accordion-body collapse'>";
echo "<div class='accordion-inner'>";
echo "<div class='control-group' style='min-height: 50px'>";
echo "<div class='row-fluid show-grid'>";
echo "<div class='span12'>";
echo "<div class='span1'>";
echo "<label>Name:</label>";
echo "</div>";
echo "<div class='span11'>";
echo "<label class='labelAccordionName'>",
$row['Name'],"</label>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='row-fluid show-grid'>";
echo "<div class='span12'>";
echo "<div class='span1'>";
echo "<label>Location:</label>";
echo "</div>";
echo "<div class='span11'>";
echo "<label class='labelAccordionLocation'>",
$row['Location'],"</label>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "<div class='row-fluid show-grid'>";
echo "<div class='span12'>";
echo "<a class='select btn'>Select</a>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
$group = $row['Name'];
}
}
}
?>
</div>
<div id="divDetails" style="display: none;">
<label>Name:</label>
<label id="labelName"></label><br />
<label>Location:</label>
<label id="labelLocation"></label><br />
<a id="close" class="btn">Close</a>
</div>
<script>
something like that, didn't check as you don't provided a jsfiddle. As for your data to retrieve correctly, I would suggest adding a class on active accordion-body then in your finds use this class to select the correct element :
$('.accordion-toggle').click(function() {
var $acc = $('#accordion2');
$('.activeAccordion').removeClass('activeAccordion');
$(''+$(this).next('.buttonAccordionToggle').data('target')).addClass('activeAccordion');
$acc.on('show', '.collapse', function() {
$acc.find('.collapse.in').collapse('hide');
});
});
then use this new class :
$('#labelName').html($('accordion-body.activeAccordion .labelAccordionName').html()));
$('#labelLocation').html($('accordion-body.activeAccordion .labelAccordionLocation').html()));

Div not looping correctly

I'm trying to get my code to display multiple divs. But seems to only display one div correctly.
<?php
// database connection
//$result = mysqli_query($con,"SELECT * FROM test WHERE field5='Cohen'");
echo " <div id='1'>";
while($row = mysqli_fetch_array($result))
{
echo "<div class='tutorName'>" . $row['field1'] . "</div>";
echo "<div class='tutorPrice'>" . $row['field2'] . "</div>";
echo "<div class='tutorInstitution'>" . $row['field3'] . "</div>";
echo "<div class='tutorLocale'>" . $row['field4'] . "</div>";
echo "<div class='tutorPhone'>" . $row['field5'] . "</div>";
}
echo "</div>";
mysqli_close($con);
?>
I want the following to loop and over using the fields above (field1, feild2, etc):
<div id='' name='' class="column threecol">
<div class="course-preview premium-course">
<div class="course-image">
<img src="img.png" />
<div class="course-price">
<div class="corner-wrap">
<div class="corner"></div>
<div class="corner-background"></div>
</div>
<div class="price-text"><span class="amount">PerHour</span></div>
</div>
</div>
<div class="course-meta">
<header class="course-header">
<h5 class="nomargin">TutorName </h5>
<div class='gender'>Gender: </div>
<div class='price-range'>Price Range: </div>
<div class='institute'> Institute: </div>
</header>
</div>
</div>
</div>
Is your $result line really commented out in the real code? Or did you just add that comment for the example? If it's commented-out in the code you're testing, then that's probably the issue.
you want this? , 1 div wrap all content like table tr
$i=1;
while($row = mysqli_fetch_array($result))
{
echo " <div id='{$i}'>";
echo "<div class='tutorName'>" . $row['field1'] . "</div>";
echo "<div class='tutorPrice'>" . $row['field2'] . "</div>";
echo "<div class='tutorInstitution'>" . $row['field3'] . "</div>";
echo "<div class='tutorLocale'>" . $row['field4'] . "</div>";
echo "<div class='tutorPhone'>" . $row['field5'] . "</div>";
echo "</div>";
$i++;
}

Categories