| 05 Sep 2008 12:32:34 pm by KermMartian |
|
Quote |
Haha, it's one of those stupid little errors; you have all the Regex stuff correct. Change:
| Code: |
| preg_replace($rep, $rep1, $temp); |
to
| Code: |
| $temp = preg_replace($rep, $rep1, $temp); |
|
| 05 Sep 2008 12:41:44 pm by lafferjm |
|
Quote |
Thanks again Kerm. Now that Kerm finished this project for me . Does anyone have any other projects ideas that i can try to work on?  |
| 05 Sep 2008 12:50:44 pm by KermMartian |
|
Quote |
Well, just as a stylistic nitpick, I'd like to improve
| Code: |
$rep[0] = "/&r/";
$rep[1] = "/&b/";
$rep[2] = "/&Y/";
$rep[3] = "/&c/";
$rep[4] = "/&R/";
$rep[5] = "/&B/";
$rep[6] = "/&O/";
$rep[7] = "/&w/";
$rep[8] = "/&g/";
$rep[9] = "/&c/";
$rep[10] = "/&p/";
$rep[11] = "/&z/";
$rep[12] = "/&G/";
$rep[13] = "/&W/";
$rep[14] = "/&P/"; |
to
| Code: |
| $rep = array("/&r/","/&b/","/&Y/","/&c/","/&R/","/&B/","/&O/","/&w/","/&g/","/&c/","/&p/","/&z/","/&G/","/&W/","/&P/"); |
|
| 25 Jun 2012 06:30:50 pm by lafferjm |
|
Quote |
So once again I have begun working with php. I am not trying to do anything spectacular, just playing around with sessions. But I have run into an issue that I know one way of solving but was curious as to whether or not there is a better way. My code is as follows:
| Code: |
<?php
session_start();
require_once("../includes/db.php");
//If user name is set they are logged in
if(isset($_SESSION['username'])) {
//If userlevel is not 0 then they are not an admin and need to leave
if($_SESSION['userlevel'] != 0) {
die("Sorry you do not have permission to be here.");
}
//If we get here they are an admin and we can do stuff
echo "Welcome to the admin area " . $_SESSION['username'] . ".";
//Now we need to connect to the database so that we can retrieve all users
mysql_connect(DB_HOST,DB_USER,DB_PASS) or die("Could not connect to database");
mysql_select_db(DB_NAME) or die("Could not select database");
$query="SELECT * FROM members WHERE userlevel='1'";
$rows=mysql_query($query);
echo "<table border='1px'>";
while($results=mysql_fetch_array($rows)) {
echo "<tr>";
echo "<td>" . $results['username'] . "</td>";
echo "<td><a href='" . $_SERVER['PHP_SELF'] . "?action=promote&user=" . $results['username'] . "'>promote</a></td>";
echo "</tr>";
}
echo "</table>";
} else {
echo '<div>You must login first. Please click ';
echo '<a href="../index.php">here</a>';
echo ' to login.</div>';
}
?>
|
$_SERVER['PHP_SELF'] points to itself(http://localhost/~lafferjm/login/admin/admin.php?. Which is close to what I want, but instead of admin.php I want actions.php. I know document_root could work, but with me using userdir, it doesn't work correctly. Another solution would be just to hardcode the url but I wish to avoid that. The solution that I have thought of involves simple string processing to get the root, but it seems as though there should be a bit of an easier way.
EDIT: After making the post I figured maybe the error was with the way userdir was configured and found a solution via that route. In the above code I replaced
| Code: |
| echo "<td><a href='" . $_SERVER['PHP_SELF'] . "?action=promote&user=" . $results['username'] . "'>promote</a></td>"; |
with
| Code: |
echo "<td><a href='" . dirname($_SERVER['PHP_SELF']) . "/actions.php?action=promote&user=" . $results['username'] . "'>promote</a></td>";
|
and all is well. |
| 25 Jun 2012 06:45:52 pm by KermMartian |
|
Quote |
Yup, that's the solution that I was about to suggest before I saw in SAX that you had edited your post. Nice job on the four-year necrobump on your own thread with relevant content, which might be a record. Of course, feel free to post if you have further questions or thoughts. |
| 26 Jun 2012 10:24:32 pm by lafferjm |
|
Quote |
| Will do. I am going to attempt to recreate the same thing but with an oo approach, because I can see how the procedural way can get ugly fast. I also want to try and learn a little about pdo. And as a side note from reading my previous posts I must have been an annoying little a back in the day. |
| 29 Jun 2012 06:04:07 pm by lafferjm |
|
Quote |
| Alright so I have rewritten the entire thing using pdo. My next attempt will be using objects. After the first login I will build the user object with the retrieved data. My question is, should I store this data in a session or should I run the query and rebuild the object on every page? |
| 30 Jun 2012 01:45:42 am by allynfolksjr |
|
Quote |
| Run from PHP. Runnnn. |
| 01 Jul 2012 07:27:03 am by KermMartian |
|
Quote |
| lafferjm wrote: |
| Alright so I have rewritten the entire thing using pdo. My next attempt will be using objects. After the first login I will build the user object with the retrieved data. My question is, should I store this data in a session or should I run the query and rebuild the object on every page? |
There's nothing wrong with storing everything in sessions, and since it's easier, I say that you do that. |