FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Mysql admin area? please help





ncwdavid
Hey I have just made a registration system for my site and now i want to make an admin area. My registration system is made from php and mysql. I have had a go at creating a simple looking admin area but it does not work. This is the code:
Code:

<?php
include('db.php');

$sql_first_name = mysql_query("SELECT first_name FROM members");
$first_name = mysql_fetch_row($sql_first_name);
$sql_last_name = mysql_query("SELECT last_name FROM members");
$last_name = mysql_fetch_row($sql_last_name);
$sql_username = mysql_query("SELECT username FROM members");
$username = mysql_fetch_row($sql_username);
$sql_email_address = mysql_query("SELECT email_address FROM members");
$email_address = mysql_fetch_row($sql_email_address);
$sql_password = mysql_query("SELECT password FROM members");
$password = mysql_fetch_row($sql_password);
$sql_activate = mysql_query("SELECT activate FROM members");
$activate = mysql_fetch_row($sql_activate);
$sql_signup_date = mysql_query("SELECT signup_date FROM members");
$signup_date = mysql_fetch_row($sql_signup_date);
$sql_last_login = mysql_query("SELECT last_login FROM members");
$last_login = mysql_fetch_row($sql_last_login);
?>
<html>
<head>
<title>Table practice</title>
</head>
<body>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Email Address</th>
<th>Password</th>
<th>Activate</th>
<th>Signup Date</th>
<th>Last Login</th>
</tr>

<tr>
<td><?php echo $first_name[0]?></td>
<td><?php echo $last_name[0]?></td>
<td><?php echo $username[0]?></td>
<td><?php echo $email_address[0]?></td>
<td><?php echo $password[0]?></td>
<td><?php echo $activate[0]?></td>
<td><?php echo $signup_date[0]?></td>
<td><?php echo $last_login[0]?></td>
</tr>
</table>
</body>
</html>


Here is my problem:
It only shows the very first person in the table and their stats. I want it to show all the members going down in each row. I know that i could add on $first_name[1],$first_name[2] and so on but i dont want to have to edit my admin.php file everytime to see new members. How can this be done? thank you.
hexkid
ncwdavid wrote:
Here is my problem:
It only shows the very first person in the table and their stats.


Code:

<?php
include('db.php');

## $sql_first_name = mysql_query("SELECT first_name FROM members");
## $first_name = mysql_fetch_row($sql_first_name);
## $sql_last_name = mysql_query("SELECT last_name FROM members");
## $last_name = mysql_fetch_row($sql_last_name);
## $sql_username = mysql_query("SELECT username FROM members");
## $username = mysql_fetch_row($sql_username);
## $sql_email_address = mysql_query("SELECT email_address FROM members");
## $email_address = mysql_fetch_row($sql_email_address);
## $sql_password = mysql_query("SELECT password FROM members");
## $password = mysql_fetch_row($sql_password);
## $sql_activate = mysql_query("SELECT activate FROM members");
## $activate = mysql_fetch_row($sql_activate);
## $sql_signup_date = mysql_query("SELECT signup_date FROM members");
## $signup_date = mysql_fetch_row($sql_signup_date);
## $sql_last_login = mysql_query("SELECT last_login FROM members");
## $last_login = mysql_fetch_row($sql_last_login);
?>
<html>
<head>
<title>Table practice</title>
</head>
<body>
<table border="1">
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Username</th>
<th>Email Address</th>
<th>Password</th>
<th>Activate</th>
<th>Signup Date</th>
<th>Last Login</th>
</tr>

<?php

## <tr>
## <td><?php echo $first_name[0]?></td>
## <td><?php echo $last_name[0]?></td>
## <td><?php echo $username[0]?></td>
## <td><?php echo $email_address[0]?></td>
## <td><?php echo $password[0]?></td>
## <td><?php echo $activate[0]?></td>
## <td><?php echo $signup_date[0]?></td>
## <td><?php echo $last_login[0]?></td>
## </tr>

$sql = "select first_name, last_name, username, email_address, password, activate, signup_date, last_login from members";
$resource = mysql_query($sql) or die('Error in query: ' . mysql_error());
while ($row = mysql_fetch_row($resource)) {
    echo '<tr>';
    foreach ($row as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';
}
mysql_free_result($resource);

// and you should now also close the connection to the database :)

?>

</table>
</body>
</html>
ncwdavid
Thanks for that. it works great but I dont understand this bit:
Code:

foreach ($row as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';
}
mysql_free_result($resource);


What is the ($row as $value) bit about? What is $value and what does the function mysql_free_result($resource) do? It all works but i just want to know how it all works incase i want to change it.
hexkid
ncwdavid wrote:
Thanks for that. it works great but I dont understand this bit:
Code:

foreach ($row as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';
}
mysql_free_result($resource);

What is the ($row as $value) bit about? What is $value [...]?

The foreach() construct will use each array value inside the loop, much like a standard for() construct.
Code:
<?php
for ($i=0; $i<10; $i++) {
  echo $i;
}
?>

In the code above, the "echo $i;" instruction will be executed ten times, with the value of $i changing from 0 (zero) to 9.
Code:
<?php
$data = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
foreach ($data as $value) {
  echo $value;
}
?>

In the code above, the "echo $value;" instruction will be executed ten times, with $value changing from 0 (zero) to 9, just like the for() loop in the first example.
foreach() is better because you don't need to know how many items are in the array. PHP will deal with that all by itself. Compare
Code:
<?php                                 |**|    <?php
$data = array(0, 1, 2, 3);            |**|    $data = array(0, 1, 2, 3);
                                      |**|
for ($i=0; $i<4; $i++) {              |**|    foreach ($data as $value) {
  echo $data[$i];                     |**|      echo $value;
}                                     |**|    }
?>                                    |**|    ?>

And there are arrays which have "holes" (not ones created with mysql_fetch_row())
Code:
<?php                                 |**|    <?php
error_reporting(E_ALL)                |**|    error_reporting(E_ALL);
ini_set('display_errors', '1');       |**|    ini_set('display_errors', '1');
                                      |**|
$data = array(0, 1, 4=>2, 3);         |**|    $data = array(0, 1, 4=>2, 3);
                                      |**|
for ($i=0; $i<4; $i++) {              |**|    foreach ($data as $value) {
  echo $data[$i]; /* ERROR */         |**|      echo $value;
}                                     |**|    }
?>                                    |**|    ?>

And there are arrays without numeric indexes Smile
Code:
$data = array('id'=>42, 'name'=>'hexkid', 'eyecolor'=>'brown');

For these arrays you can use the second syntax for foreach()
Code:
<?php
$data = array('id'=>42, 'name'=>'hexkid', 'eyecolor'=>'brown');
foreach ($data as $key=>$value) {
  echo "key: $key; value: $value<br>\n";
}
?>



ncwdavid wrote:
[...] what does the function mysql_free_result($resource) do?

It releases the memory PHP used to hold the data from MySQL. Only really useful if your script doesn't end until some time later because when the script ends the memory is released anyway. I just like to do it explicitly even for cases where it's (almost) irrelevant.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
The online PHP manual is really great. If you have a question about a function that you know the name of, just go to
http :// php . net / <FUNCTION_NAME>

For example
http://php.net/foreach
http://php.net/mysql_free_result
http://php.net/explode
http://php.net/echo
...

[EDIT]: Of course, asking here is good too, but often, for many questions, it's faster to look at the manual.


Happy Coding Smile
Screamer
hexkid wrote:
ncwdavid wrote:
Here is my problem:
It only shows the very first person in the table and their stats.


Code:

<?php
$sql = "select first_name, last_name, username, email_address, password, activate, signup_date, last_login from members";
$resource = mysql_query($sql) or die('Error in query: ' . mysql_error());
while ($row = mysql_fetch_row($resource)) {
    echo '<tr>';
    foreach ($row as $value) {
        echo "<td>$value</td>";
    }
    echo '</tr>';
}
mysql_free_result($resource);
?>



Very nice advice... 10xThanks
foreach is great feature!
ncwdavid
All that works now and i looked at your little tutorial above and i understand it now. But now i want to be able to have a little box next to each row and i am able to click on it and a thick or whatever appears and then have a few features at the bottom of all the rows like delete,edit etc. How can that be done? even a bit of that would be helpful in getting me on the track to completeing it. Thanks.
ncwdavid
Its alright now. I figured one out myself and its really easy to user and works really well.
Related topics
php admin and mysql admin console
Cant access admin area after updateing to 2.0.15
simple php problem, please help...
phpBB2 ERROR PLEASE HELP
please help me, i don't know anything here...
Problem with MySQL Server Please Help!!
mysql base error please help
A very good PHP MySQL Tutorial
THIS IS DRIVING ME INSANE!! PLEASE HELP!
Need Expert help in php-mysql
PHP MySQL Question
php-mysql source code
windows mobile programming and php, combination possible?
How can I log in to MySQL? Please help!
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.