Content Count

Joined

Last visited

Community Reputation

About lewashby

While following along the lessons I forgot I had moved all the files in the previous working lesson into a sub directory 'old' including the jquery file. I feel stupid, and it won't be the last time. Thanks for your help Jacques1. It is solved.

I see the 404 error and I also see that is says '$' is not defined. I'm following along with a web dev course on udemy.com and my pages look identical to his so I don't understand what the problem is. I thought $(document).ready() was valid syntax for a .js file?

My browser is loading the webpage just fine and there are no errors reported in /var/log/apache/error.log but I can't get this simple jQuery alert to show up after refreshing the browser.
Do you see anything wrong with me code? Thanks.
HTML <-index.html
<!DOCTYPE html>
<html>
<head>
<title>jQuery Content Slider</title>
<link rel='stylesheet' href='css/style.css'>
<script src='jquery-3.0.0.min.js'></script>
<script src='js/script.js'></script>
</head>
jQuery <- js/script.js
$(document).ready(function() {
alert('Doc is ready');
});

In the following pages I'm trying to validate if a user is signed in or not. If the user is signed in I would like to see 'Log Out' printed to the screen(I'll do more with that later). If the user is not signed in I would like to see a login form at the top right of the screen. As it stands I'm only seeing 'Log Out' on the screen, I can't get the form to show up anymore. I thought it might be because the session variable was still hanging around but I restarted my computer to make absolutely sure but I'm still just getting 'Log Out'. At the moment I need this program to work as is as much as possible. If you see an entirely different approach that you would use that's fine but I don't currently have the time to go changing a lot, I need to get this going kinda quick. Thanks.
records-board.php
<?php
require_once('includes/init.php');
if(!isset($_SESSION)) { init_session(); }
?>
<html>
<head>
<Title>Pop Report</title>
<link rel="stylesheet" type="text/css" href="styles/popreport2.css">
<h1>Pop Report</h1>
</head>
<body>
<?php
if(isset($_POST['nameinput']) && isset($_POST['passinput']))
{
$nameinput = $_POST['nameinput'];
$passinput = $_POST['passinput'];
User::sign_in($nameinput, $passinput);
}
if(!isset($_SESSION['user']))
{
print_form();
}
else
{
echo "Log Out ";
echo $_SESSION['user']->name; // this line was just trouble shooting, it told me nothing!
}
?>
user.php
<?php
if(!isset($_SESSION)) { init_session(); }
class User
{
public $name;
public function __construct($username, $password)
{
$connection = get_db_connection();
$query = $connection->query("SELECT * FROM users WHERE username='$username' AND password='$password'");
if(!$query)
{
echo "Invalid username or password";
}
else
{
$result = $query->fetch(PDO::FETCH_ASSOC);
if(!$result['username'] == $username || !$result['password'] == $password)
{
echo "Invalid username or password";
}
else { $this->name = $result['username']; }
}
}
public static function sign_in($username, $password)
{
$_SESSION['user'] = new User($username, $password);
}
}
?>
<?php
function print_form()
{
echo "<form id='loginform' name='loginform' action='records-board.php' method='post'>";
echo "Username: <input type='text' name='nameinput'>";
echo "Password: <input type='text' name='passinput'><br />";
echo "<input type='submit' value='Sign In'>";
echo "</form>";
}
?>

If I have a php file that generates an html form from another php file which file would be the default file that would try to execute if I leave the action field blank in the html form? The php file that the form sits in or the php file that it's called from? Thanks.

<?php
require_once('init.php');
function print_rows($inmates)
{
foreach($inmates as $inmate)
{
echo "<tr>";
echo "<td><span class='rightpadding'>$inmate->get_property('first_name');</span>$inmate->get_property('number');<span class='rightpadding'>(W)</span>Facility<br /><br /><span class='dates'>8-05-15 <span class='red'>/</span> 8-08-15</span><a href=''><span class='edit'>Edit</span></a></td>";
echo "</tr>";
}
}
?>
In the code above my inmate object method calls are being displayed a literal strings in the output. How can I get these to expand in this string? I also tried concatenation with the . operator but the page wouldn't load at all using that method.

When I load this into the browser I get a bunch of zeros '0' stacked on top of each other on the left side of the screen instead of getting the name and numbers of the individuals. I added this line in one of the files to try and find the problem but as it turns out this line is outputting exactly what I wanted and expected it to output so I don't know why the rest of the program isn't working.
print_r('The $attribute variable = ' . $attribute . '<br />'); <- that's the line I added into the file inmate.php and it shows that the variable $attribute has exactly what it needs to have but when I try and output this information from the test.php file I get the zeros.
test.php
<?php
require_once("./database.php");
require_once("./inmate.php");
$inmates = Inmate::get_inmates();
foreach($inmates as $inmate)
{
print $inmate->inmate_property['first_name'] . " " . $inmate->inmate_property['last_name'] . " " . $inmate->inmate_property['number'] . "<br />";
}
?>
inmate.php
<?php
require_once("./database.php");
class Inmate
{
// public $first_name;
// public $last_name;
// public $full_name;
// public $race;
// public $number;
// public $facility;
// public $type_of_transit;
public $inmate_property = array('first_name' => '', 'last_name' => '', 'full_name' => '', 'race' => '', 'number' => 0, 'facility' => '', 'type_of_transit' => '');
public function __construct($result)
{
// $this->first_name = $result['first_name'];
// $this->last_name = $result['last_name'];
// $this->number = $result['number'];
foreach($result as $attribute)
{
print_r('The $attribute variable = ' . $attribute . '<br />');
$this->inmate_property[$attribute] = $attribute;
}
}
public function get_property($property)
{
return $this->$property;
}
public function set_property($property, $value)
{
$this->$property = $value;
}
public static function get_inmates()
{
$connection = get_db_connection();
$inmates = array();
$sql = "SELECT * FROM inmate_board";
$query = $connection->query($sql);
while($result = $query->fetch(PDO::FETCH_ASSOC))
{
array_push($inmates, new Inmate($result));
}
return $inmates;
}
}
?>
I'm trying to setup xdebug for vim so I don't have to post this many questions but I'm having some trouble getting that going.

[Linux]
PHP Notice: Undefined variable: connection in /var/www/html/popreport/functions.php on line 23
PHP Fatal error: Call to a member function query() on a non-object in /var/www/html/popreport/functions.php on line 23
The fuction output in the following program is called from another file called records records-board.php
If you look at the program below you'll see that I did define $connection above line 23 in the file functions.php
And for the second error I'm really not getting it because that same foreach loop was working fine with the exact
same argument list when it was in the file records-board.php, but now that I've copied most of the code from
records-board.php and placed it in functions.php all the sudden my program can't see the variable $connection
and has a problem with my foreach loop on line 23. Again, both of those lines worked fine when they were in
another file.
functions.php
<?php
//session_start();
// open a DB connectiong
$dbn = 'mysql:dbname=popcount;host=127.0.0.1';
$user = 'user';
$password = 'password';
try
{
$connection = new PDO($dbn, $user, $password);
}
catch (PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$sql = "SELECT full_name, tdoc_number, race, facility FROM inmate_board WHERE type = 'COURT'";
function output()
{
foreach($connection->query($sql) as $row)
{
echo "<tr><td>$row[full_name]</td></tr>";
}
}
?>
records-board.php
<?
include 'functions.php';
php output();
?>
Any ideas?