PHP include(); not including file - php

I am trying to include a separate snippet of code to fetch data from MySQL tables. My page code:
<?php session_start(); ?>
<html>
<?php include('head.php'); ?>
<body>
<?php include('navigation.php'); ?>
<div id="container">
<?php
$section = "Movies";
print "THIS IS A TEST";
//$_SESSION['sectiontemp'] = $section;
include('section-grabinfo.php');
include('footer.php');
?>
</div>
</body>
</html>
My section-grabinfo.php page:
<?php
print "THIS IS A TEST";
$sql = mysqli_query($conn, "SELECT * FROM article JOIN person ON article.author=person.id WHERE section='".$section."' AND status=1 ORDER BY aid DESC LIMIT 1;");
if ($sql == 'false') {
print "SQL doesn't work";
}
elseif ($sql == 'true') {
print "Works all fine";
}
else {
print "Wrong code.";
}
while ($row = mysqli_fetch_assoc($sql)) {
$title = $row['title'];
$preview = $row['preview'];
$author = $row['name'];
$person_id = $row['author'];
$id = $row['id'];
$username = $row['username'];
include('featured-article.php');
}
//LEAVE SPACE FOR ADS
?>
<div id="secondaryArticleSection">
<?php
$sql2 = mysqli_query($conn, "SELECT * FROM article JOIN person ON article.author=person.id WHERE aid<(SELECT max(aid) FROM article WHERE section='."$section."' AND status=1) AND section='".$section."' AND status=1 ORDER BY aid DESC;");
while ($row = mysqli_fetch_assoc($sql2)) {
$title = $row['title'];
$preview = $row['preview'];
$author = $row['name'];
$person_id = $row['author'];
$id = $row['id'];
$username = $row['username'];
include('secondary-article.php');
}
?>
</div>
Basically my problem is that section-grabinfo.php and footer.php are not being included on the main page. Please keep in mind that this worked completely when I had this on my laptop computer (not the server I'm currently working with). Thank you.

If it is really an including trouble, you could try to include this way
<?php include(__DIR__.'/head.php'); ?>

Related

Nested while loop in PHP in getting data from database

<?php
$sqlquerypmenu = "select *
from subsubmenu
where submenu_id=1
and position='left'
and status=1";
if($querypmenu = sqlsrv_query($conn,$sqlquerypmenu)){
if(sqlsrv_has_rows($querypmenu) === true){
while($rowdata = sqlsrv_fetch_array($querypmenu, SQLSRV_FETCH_ASSOC)){
?>
<h4 class="title-small folder_name"> <?php echo $rowdata ['website_title']; ?> </h4>
<?php
$id = $rowdata['id'];
$filequerymenu = "select *
from upload_files
where main_menu='value_name'
and sub_menu='value_key'
and subsub_menu= $id ";
if($filemenu = sqlsrv_query($conn,$filequerymenu)){
if(sqlsrv_has_rows($filemenu) === true){
while($filedata = sqlsrv_fetch_array($filemenu, SQLSRV_FETCH_ASSOC)){ ?>
<a class="smalltext font_val" href="<?php echo DOCUMENT_URL.$filedata ['file_name']; ?> " target="_blank" ><?php echo $filedata['document_name']; ?></a>
<?php
}
}
}
}
}
}
?>
In the nested while loop the second while loop only shows the first row of data and does not show the rest of the data.
How can I fix this?
Consider querying the database once with one JOIN query. Possibly opening up another fetch within a while loop causes instances issues:
<?php
...
$sql = "select s.website_title, u.file_name, u.document_name
from upload_files u
inner join subsubmenu s ON u.subsub_menu = s.id
where u.main_menu = 'value_name'
and u.sub_menu = 'value_key'
and s.submenu_id = 1
and s.[position] = 'left'
and s.[status] = 1
order by s.id, s.website_title;"
$title = "";
if($result = sqlsrv_query($conn, $sql)){
if(sqlsrv_has_rows($result) === true){
while($rowdata = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)){
if($title != $rowdata['website_title']) {
$title = $rowdata['website_title']
?>
<h4 class="title-small folder_name"> <?php echo $rowdata ['website_title']; ?> </h4>
<?php
}
?>
<a class="smalltext font_val" href="<?php echo DOCUMENT_URL.$filedata ['file_name']; ?> " target="_blank" ><?php echo $filedata['document_name']; ?></a>
<?php
}
}
}
?>

select all not working in mysql

pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out.
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
?>
<?php
}
?>
<?php
echo $id;
echo $body;
?>
Either print the values in while loop or store them in an array to print them outside the loop.
while($row = mysql_fetch_assoc($query)){
echo $row["id"];
echo $row["body"];
}
condb.php
<?php
$con = mysqli_connect("localhost","username","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>
index.php
<?php
include 'database/condb.php';
$query = "SELECT * FROM posts";
$res = mysqli_query($con, $query);
while($row = mysqli_fetch_assoc($res)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo $body;
}
?>
For storing data, sometimes using array becomes helpful. I usually recommend it:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
$array[$id] = ['username'=>$username, 'body'=>$body, 'date_added'=>$date_added, 'hasttags'=>$hasttags];
}
?>
<?php
foreach($array as $id => $value){
echo $id; //Prints id
echo "<br>";
echo $value['username'].", ".$value['body'].", ".$value['date_added'].", ".$value['hasttags'];
}
?>
Or you can simply do this:
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts");
$array = [];
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags = $row["hashtags"];
?>
<?php
echo $id.", ".$username.", ".$body.", ".$date_added.", ".$hasttags;
}
?>
<?php
include 'database/condb.php';
$query = mysql_query("SELECT * FROM posts ");
while($row = mysql_fetch_assoc($query)){
$id = $row["id"];
$username = $row["username"];
$body = $row["body"];
$date_added = $row ["date_added"];
$hasttags= $row["hashtags"];
echo $id;
echo "<br>";
echo $body;
?>
<?php
}
?>
As an answer to the question "pls i tried to display all content in a database, it keeps dispaying the last inserted, kindy help out."
Put echo $id, echo $body be inside the loop.

PHP/MySQL double loop

I am working on a directory where some of the listings have a images associated with them and others do not. I am wondering how I can write a loop within a loop to get my results.
Example, User selects state they want results from, query goes to DB requesting all listings in that state.
<?php
if (isset($_POST['searchButton'])) {
$state = $_POST['state'];
$query = "SELECT * FROM directory LEFT JOIN directory_images ON directory.id = directory_images.user_id WHERE directory.state = '$state' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) == 0) {
echo "<p>Sorry, there are no listings in '$state', check back soon!</p>\n";
}
else
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$name = $row['name'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$has_support_pics = $row['file_name'];
?>
<h4><?php echo $name ?></h4>
<p><?php echo $address ?><br/>
<?php echo $city . ' ' . $state . ', ' . $zip; ?><br/>
</p>
<?php
// check to see if ID has extra images
if (isset($has_support_pics)) {
$query2 = "SELECT file_name FROM directory_images WHERE user_id = '$id'";
$result2 = mysql_query($query2) or die(mysql_error());
echo $query2.'<br/>';
?>
<ul class="support_images">
<?php
while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
$support_image = $row['file_name'];
echo $support_image.'<br/>';
}
?>
</ul>
</div>
<br/>
</div>
<?php
}
echo "<hr/>";
}
}
?>
Do NOT run queries in loops - use a join.
Here is a tutorial: http://thewebmason.com/tutorial-parent-child-lists/

Using echo with lists in php?

I have the following PHP code:
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
echo "<div class=\"title\">$title</div><br>";
echo nl2br($body);
echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
echo "<hr>";
}
This is used to create a news feed and I use echo to print out what is within the updates.
Is there a way in which I could maybe use a list to print out each update instead of the way im currently doing it?
Or is it possible to create a div around each update that the while loop creates?
I'm sorry if the question is not clear but thanks for all the help!
My newsfeed creates updates in a news feed like twitter. Each update is printed out using echo and surrounded by . Im trying to find a way in which I can create a list or div for the entire layout of each update. Im finding very difficult to arrange whats going on in each update.
The following code should get you there. Just echo out the html as you would like it to appear.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
echo "<div class='news-article'>";
echo "<div class=\"title\">$title</div><br>";
echo nl2br($body);
echo "<br><div class=\"date_time\">".time_ago($date)."</div>";
echo "</div>";
echo "<hr>";
}
A lot of the time a lot of echo statements like that just confuse what you are trying to do.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<div class='news-article'>
<div class="title"><?php echo $title ?></div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</div>
<hr>
<?php
}
If you wanted to accomplish the same thing as an unordered list you would do this.
$getnews = mysql_query("SELECT * FROM news ORDER BY id DESC") or die(mysql_error());
echo "<ul class='article-list'>";
while ($row = mysql_fetch_assoc($getnews)) {
$id = $row['id'];
$title = $row['title'];
$body = $row['body'];
$date = $row['date'];
?>
<li class='news-article'>
<div class="title">$title</div><br>
<?php echo nl2br($body); ?>
<br><div class="date_time"><?php echo(time_ago($date)) ?></div>
</li>
<?php
}
echo "</ul>";

Building a search results page via php

I need to build HTML page to show and organize my results. Where do I place the HTML? I tried it and I get errors. Does it go inside the php script? after the php script? before? Confused.
<?php
if (isset($_POST['Submit'])) {
if (!empty($_POST['reg'])) {
$record = $_POST['reg'];
$query = mysql_query("SELECT * FROM reg_add WHERE reg='" . mysql_real_escape_string($record) . "'");
$result = mysql_num_rows($query);
if ($result != 0) {
$row = mysql_fetch_array($query);
$connect_date = $row['connect_date'];
$reg = $row['reg'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$nickname = $row['nickname'];
$gender = $row['gender'];
$birthday = $row['birthday'];
$home_state = $row['home_state'];
$national = $row['national'];
$location = $row['location'];
} else {
header("Location: search_error1.php");
exit;
}
} else {
header("Location: search_error2.php");
exit;
}
}
?>
PHP is embeddable into HTML, so you can write your html markup right in php file. You just need to place all your php code inside <?php ?>:
<?php
//php code
?>
<html>
<!-- ... -->
</html>
<?php /* php again */ ?>
PHP interpreter simply ignores all text written outside the php tag (<?php ?>) and writes it directly to stdout.
So for you it will be something like this:
<?php
// ...
if ($result != 0) {
while($row = mysql_fetch_array($query)) {
$connect_date = $row['connect_date'];
$reg = $row['reg'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$nickname = $row['nickname'];
$gender = $row['gender'];
$birthday = $row['birthday'];
$home_state = $row['home_state'];
$national = $row['national'];
$location = $row['location'];
?>
First name: <?php echo $first_name ?><br/>
Last name: <?php echo $last_name ?><br/>
...
<?php
}
}
// ...
?>

Categories