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);
Thanks again Kerm. Now that Kerm finished this project for me Rolling Eyes . Does anyone have any other projects ideas that i can try to work on? Laughing
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/");
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.
Yup, that's the solution that I was about to suggest before I saw in SAX that you had edited your post. Smile 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.
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 shit back in the day.
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?
Run from PHP. Runnnn.
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.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
» Goto page Previous  1, 2, 3, 4, 5, 6, 7, 8
» View previous topic :: View next topic  
Page 8 of 8
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement