homepage Welcome to WebmasterWorld Guest from 54.211.230.186
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
New to php and need a little help
jaredwins



 
Msg#: 4453935 posted 10:46 pm on May 15, 2012 (gmt 0)

Hey guys, I'm working on building a PHP with MySQL site for where I work. We just need something simple to display some printing reports. I signed up for this forum because I'm using a loop statement that jatar_k posted.
Now what I would like this to do is query the database grabbing all of our Departments, and list them separately with a check mark box beside them, once that is working I will have a submit button that POSTs the checked values and runs another query to display the list of printer reports based on the departments the user checked marked.

Here's my php code that is working but only one department is getting the value type.


<?php
require('function_db_connection.php');


$query = "SELECT * FROM departments ORDER BY department";
$result = mysql_query($query);

$tdcount = 1;
$numtd = 4; // number of cells per row
echo "<table>";
while($row = mysql_fetch_array($result)) {
if ($tdcount == 1)
//something wrong here--
echo "<td><input type=\"checkbox\" name=\"department\" value=\"";
echo $row['department_id'];
echo "\" />";
echo $row['department'];
echo "</td>";
//--to here
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;
}
}
// close the table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
echo "</table>";
echo "</body>";
echo "</html>";

require ('footer.php');
?>


And here is my page source code where it's failing. I'm sure I'm just missing a quote but I cannot find it!

<table><td><input type="checkbox" name="department" value="2" />Administration</td>3" />ER</td>4" />OR</td><td>&nbsp;</td></tr></table></body></html>


Thanks for any help or advice, I'm still very new to PHP and HTML.
-Jared

 

matrix_jan



 
Msg#: 4453935 posted 11:21 pm on May 15, 2012 (gmt 0)

Welcome to WebmasterWorld jaredwins,

//something wrong here--

{ is missing :)

//--to here

close the if statement with }

Also I think you should have $tdcount <= $numtd instead of $tdcount == 1. In your script 2nd 3rd and 4th td-s are being ignored (?!)

Good luck

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4453935 posted 11:50 pm on May 15, 2012 (gmt 0)

While that's good advice, PHP will still work with unbracketed blocks - but the second part is correct . . . it's because you're only echoing if it's 1, and it's only 1 **once.**

$tdcount = 1; So it starts off as 1, right?

while($row = mysql_fetch_array($result)) {
if ($tdcount == 1) { The brackets might have made it more obvious
// so only if it's **1**, echo something.
} // ending bracket
if ($tdcount == $numtd) {
//all the while it's NOT equal to $numtd yet . . . make it greater than 1!
} else {
$tdcount++;
}

See it? You're only echoing if it's 1, otherwise it just increments in which case it's no longer 1. :-)

I use a different method, but to "fix" this one,


while($row = mysql_fetch_array($result)) {
if ($tdcount <= $numtd) { // As mentioned ,less than or equal to . . .
echo "<td><input type=\"checkbox\" name=\"department\" value=\"" .
$row['department_id'] . "\" />" . $row['department'] . "</td>";
}
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else { $tdcount++; }
}

jaredwins



 
Msg#: 4453935 posted 2:03 pm on May 16, 2012 (gmt 0)

Thank you guys so much! You were right, it was only echoing once, upon changing that everything works :)
Now to move on to formatting it into a table with a border and adding in more query stuff... I'm sure I'll be asking more questions soon!

jaredwins



 
Msg#: 4453935 posted 3:07 pm on May 18, 2012 (gmt 0)

Alright I have another issue that another guy and I have been working on the past day and cannot get it to function correctly.
So in my header form this is a chunk of my code for searching reports. The issue seems to be that an array is being created dynamically for checkboxes but the selected values are not passing to the printer_info page where my SELECT statement gets run.

<center>
<form action="printer_info.php" method=get>
<table border="2">
<b>Departments</b>
<tr>
<?php
require('function_db_connection.php');
$query = "SELECT * FROM departments ORDER BY department";
$result = mysql_query($query);
$tdcount = 1;
// number of cells per row
$numtd = 1;
/*Grabs the Departments*/
while($row = mysql_fetch_array($result)) {
if ($tdcount <= $numtd) {
echo "<td><input type=\"checkbox\" name=\"department[" . $row['department_id'] . "]\" id = \"dept_";
echo $row['department_id'];
echo "\" value = \"1\">";
echo $row['department'];
echo "</td>";
}
if ($tdcount == $numtd) {
echo "</tr>";
$tdcount = 1;
} else {
$tdcount++;}
}
// close table
if ($tdcount!= 1) {
while ($tdcount <= $numtd) {
echo "<td>&nbsp;</td>";
$tdcount++;
}
echo "</tr>";
}
?>

</td>



This _GETs to this page, which I know is not the correct way to pass this info but it helps see the variables and numbers in the URL.

<?php
require('header.php');
/* $query = "select * from department";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$dept_ */
//$deptid = $_GET['department'];
//$deptid = $HTTP_POST_VARS['department'];
/*$dept_id = $_POST['department'];
if(empty($dept_id)) {
$dept_id = "%";
} else {
$N = count($dept_id);
echo("You selected $N department(s): ");
for ($i=0; $i < $N; $i++)
{
echo ($dept_id[$i] . " ");
}
} */
//$deptid = implode(",",$_POST['department']);

foreach ($_POST['department'] as $key => $value) {
echo "Key: $key; Value: $value<br>";
}

$dstart = $_GET['start'];
$dend = $_GET['end'];

echo $deptid;
echo $dstart;
echo $dend;
echo "hello!";
/*starting new table to store that DATA*/


Here is what the URL looks like:
http://******/printer_reports/printer_info.php?department[2]=1&department[3]=1&department[4]=1&start=&end=

So the selected department ids are getting passed, but I can't figure out how to retrieve them to run my SQL query. I've tried multiple array examples, implode and explode statements but to no avail. Thanks for any help!

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4453935 posted 3:39 pm on May 18, 2012 (gmt 0)

Well, I think you're making it harder on yourself by using arrays, but if you use arrays, I'd do it another way. Try this.
echo "<td><input type=\"checkbox\" name=\"department[]\" id=\"dept_" . $row['department_id'] . " value=\"" . $row['department_id'] . "\"/></td>";

A side note: see how you're doing the ID there? You can do the same thing for name (which is what I would do) then loop through $_GET (though I'd use post) looking for keys beginning with "dept_". But let's move on with the arrays . . . .

We've put the value of the department ID into the value field instead of 1. So in your receiving script,


$deptid = array();
$deptid = $_GET['department'];
$N = count(array_keys($deptid));
if ($N > 0) {
echo "<p>You selected $N department(s): </p><ul>";
foreach ($deptid as $key => $value) {
// So now you can operate on $value for $departments[array index][deparment id]
echo "<li>Key: $key; Value: $value</li>";
}
echo '</ul>';
}
else { echo "<p>No departments selected.</p>"; }


I don't know why you need to use get, post should work just as well without ugly query strings. :-)

jaredwins



 
Msg#: 4453935 posted 4:14 pm on May 28, 2012 (gmt 0)

Thanks for all the help guys! I have nearly everything working the way I want it, next I'm working on some authentication using Active Directory. The code I have right now works when a user enters in their username and password correctly, however, you have to append the FQDN at the end. For example my username is jaredwins, but if I want to log in I have to enter 'jaredwins@domain.mycompany.net'. So what I want is my login form to pass and append the '@domain.mycompany.net' part to a username.

Here's my code so far: login.php


<html>
<body>
<?php
require ('header.php');
?>
<form method="POST" action="processlogin.php">
Username: <input type="text" name="username" size="15" /><br />
Password: <input type="password" name="password" size="15" /><br />
<div align="center">
<p><input type="submit" value="Login" /></p>
</div>
</form>


<?php
require ('footer.php');
?>
</body>
</html>


Values passed to processlogin.php:


<?php
require ('header.php');
// using ldap bind
$ldaprdn = $_POST['username']; // ldap rdn or dn
$ldappass = $_POST['password']; // associated password
// connect to ldap server
$ldapconn = ldap_connect(" :) ")
or die("Could not connect to LDAP server.");

if ($ldapconn) {

// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn, $ldappass);

// verify binding
if ($ldapbind) {
echo "LDAP bind successful :) ";
} else {
echo "LDAP bind failed...";
}

}

require ('footer.php');
?>


Thanks for the help! If you have any suggestions to help me improve this I'm open minded :)

-jared

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4453935 posted 5:14 pm on May 28, 2012 (gmt 0)

One other thing that's important but not directly related to your current problems.

The first line of your code "includes" the
header.php file. You should change the order such that you read your data from the database (or at the very least determine that there is data for this request) and store it somewhere before you start sending any HTML to the browser. This then allows you to send the correct HTTP header 404 status code if there is no database entry for the current request. It is too late to start sending HTTP headers once you've started to send the HTML part of the page.

Likewise, in your original code snippet, you had "included" the
footer.php file after the closing </html> tag. That included file should usually be located just before the closing </body> tag.
jaredwins



 
Msg#: 4453935 posted 6:20 pm on May 28, 2012 (gmt 0)

I got it working but thanks. My next challenge is starting a session after the user has authenticated using LDAP. What I would like is a simple session id that can be passed to each page on my site, if there is no session (the user is not logged in) then it directs them to the login screen. I got this working in a class but we were using a mysql database to authenticate users against and the session id was the primary key in the user's table...

<?php
require ('header.php');
// using ldap bind
$ldaprdn = $_POST['username']; // ldap rdn or dn
$ldappass = $_POST['password']; // associated password
$ldapdomain = "domain.company.net"; // Ldap domian authentication
$ldaprdn2 = $ldaprdn . "@" . $ldapdomain;
echo $ldaprdn2;
// connect to ldap server
$ldapconn = ldap_connect(" :) ")
or die("Could not connect to LDAP server.");

if ($ldapconn) {

// binding to ldap server
$ldapbind = ldap_bind($ldapconn, $ldaprdn2, $ldappass);

// verify binding
if ($ldapbind) {
echo "<center><b>LDAP bind successful...</b>";

echo "<META http-equiv=\"refresh\" content=\"3 URL=http://addprinter.php\">";
echo "<center><b>Please do not refresh this page.</b>";
echo "<center><b>You will be redirected automatically.</b>";

} else {
echo "<center><b>LDAP bind failed...</b>";
}

}

require ('footer.php');
?>

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved