Mark2024 Posted October 27 Share Posted October 27 Hello Guys, im new to this and was looking for some help. I have the following code <?php include ("styles/top.php"); ?> <div id="left"> <img src="images/john.png" height="600px" width="300px" /> </div> <div id="right"> <div id="view_box"> <ul> <li><img src="images/1.png" /></li> <!-- <li><img src="pics/2.jpg" /></li> --> <!-- <li><img src="pics/3.jpg" /></li> --> </ul> </div> <div id="button"> <ul> <!-- <li><button class="button" scroll_value="0">*</button></li> -- > <!-- <li><button class="button" scroll_value="600">*</button></li> --> <!-- <li><button class="button" scroll_value="1200">*</button></li> --> </ul> </div> <hr /> <?php if(loggedin()){ ?> <h3><font color="Yellow">Welcome, <?php echo $customer_name; ?></font> <h3>Money That Your Due Today</h3> <table border=1"> <tr> <th>FULL NAME </th> <th>DATE GOT </th> <th>MONEY DUE</th> <th>UPDATE</th> </tr> <tr> <td> </td> <td></td> <td></td> <td></td> </tr> <?php error_reporting(0); $result = mysqli_query($con,"SELECT * FROM orders WHERE OrderBy = '$username' AND Status = 'Layon'"); $totmoney = 0; $strmoney = ""; $current_date = date("d/m/Y", time()); //Gets the current date, in dd/mm/yyyy format while ($myrow = mysqli_fetch_array($result)) { //Gets the date from the current record $record_date_string = $myrow['PayDate']; //Gets the string date from the database $record_date_elements = explode("/", $record_date_string); //Seperate the date into three parts (dd, mm and yyyy) $record_date = date("d/m/Y", strtotime($record_date_elements[2]."-".$record_date_elements[1]."-".$record_date_elements[0])); //Assembles these elements in a way PHP will understand and creates a new date from the record //If the record date matches the current date, display in the table if($record_date == $current_date){ $strmoney = $myrow["Money"]; $strmoney = str_replace(',','',$strmoney); if(is_numeric($strmoney)) { settype($strmoney, "float"); $strmoney = number_format($strmoney, 2); } echo "<TR><TD><a href=\"_view.php?id=".$myrow['id']."\">".$myrow["FullName"]."</A> </TD><TD>".$myrow["GotDate"]." </TD><TD>£".$strmoney." </TD>"; echo "<TD><center><a href=\"update_order.php?id=".$myrow['id']."\"><img class=\"displayed\" alt=\"View\" src=\"images/edit.png\" width=\"175\" height=\"25\" /></a></center></TD>"; echo "</TR>"; $totmoney = $totmoney + (int)$myrow["Money"]; } } ?> <tr> <td> </td> <td></td> <td></td> <td></td> </tr> <tr> <td> </td> <td>TOTAL DUE</td> <td>£<?=number_format($totmoney, 2);?> </td> <td></td> </tr> </table> <br /> <br /><br /> <?php } else { ?> <h3>Login To Your Account</h3> <form method="post" action="login.php"> <table cellspacing="10" cellpadding="10"> <tr> <td>Username</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password" /></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Login To Your Account" /></td> </tr> </table> </form> <?php } ?> </div> <?php include ("styles/bottom.php"); ?> What im trying to do is use the below code <?php if( ($user_level == 1) ){ echo "<a href='admin.php'>Admin Panel</a> |"; } ?> i want to insert this code in to the above code so if user 1 logs in it shows the table above and if someone else logs in it will show something else just just a website link for example can anyone help me please? Quote Link to comment https://forums.phpfreaks.com/topic/325232-normal-user-and-admin-user/ Share on other sites More sharing options...
Solution mac_gyver Posted October 27 Solution Share Posted October 27 learning involves forming a theory about how something works, performing an experiment to prove or disprove the theory, observing the result of the experiment and concluding if the theory was correct or not. you will learn much better and quicker if you make an attempt at designing, writing, and testing code to see if it does what you expect. if(loggedin()) { // code for all logged in users if($user_level == 1) { // code for a logged in user with level == 1 } else { // code for a logged in user with level != 1 } } else { // code for a non-logged in user } i recommend two things - 1) use defined constants, such as define('ADMIN_USER',1);, instead of literal numbers, so that anyone reading the code can tell what the numerical values mean, 2) when conditional 'failure' code is shorter then the 'success' code, invert/complement the condition being tested and put the shorter code first. this will make your code easier to read and understand. Quote Link to comment https://forums.phpfreaks.com/topic/325232-normal-user-and-admin-user/#findComment-1638952 Share on other sites More sharing options...
LeonLatex Posted October 27 Share Posted October 27 Certainly! You can incorporate the admin link into your existing code by checking the user level after the login check. Here's how you might modify your code: Check the user level after confirming that the user is logged in. Display the admin link if the user level is 1. Provide an alternative link for other users.Here’s how you can integrate the admin link into your code: <?php include ("styles/top.php"); ?> <div id="left"> <img src="images/john.png" height="600px" width="300px" /> </div> <div id="right"> <div id="view_box"> <ul> <li><img src="images/1.png" /></li> <!-- <li><img src="pics/2.jpg" /></li> --> <!-- <li><img src="pics/3.jpg" /></li> --> </ul> </div> <div id="button"> <ul> <!-- <li><button class="button" scroll_value="0">*</button></li> --> <!-- <li><button class="button" scroll_value="600">*</button></li> --> <!-- <li><button class="button" scroll_value="1200">*</button></li> --> </ul> </div> <hr /> <?php if (loggedin()) { echo "<h3><font color='Yellow'>Welcome, {$customer_name}</font></h3>"; if ($user_level == 1) { echo "<a href='admin.php'>Admin Panel</a> | "; } else { echo "<a href='user_dashboard.php'>User Dashboard</a> | "; } echo "<h3>Money That Your Due Today</h3>"; echo '<table border="1"> <tr> <th>FULL NAME</th> <th>DATE GOT</th><th>MONEY DUE</th> <th>UPDATE</th> </tr>'; // Your existing code for fetching and displaying order data continues here... // (Insert your existing code for fetching data from the database and displaying it) echo '<tr> <td> </td> <td>TOTAL DUE</td> <td>£' . number_format($totmoney, 2) . ' </td> <td></td> </tr>'; echo '</table><br />'; } else { echo '<h3>Login To Your Account</h3>'; echo '<form method="post" action="login.php"> <table cellspacing="10" cellpadding="10"> <tr> <td>Username</td> <td><input type="text" name="username" /></td> </tr> <tr> <td>Password</td> <td><input type="password" name="password" /></td> </tr> <tr> <td></td> <td><input type="submit" name="submit" value="Login To Your Account" /></td> </tr> </table> </form>'; } ?> </div> <?php include ("styles/bottom.php"); ?> Explanation:User Level Check: After confirming the user is logged in, you check if $user_level is 1. If it is, you display the admin link. If not, you can display a different link (like a user dashboard link).Dynamic Links: This makes your application more dynamic, allowing you to cater different content based on user roles. Feel free to adjust the link destinations (admin.php, user_dashboard.php, etc.) to fit your application's structure! Quote Link to comment https://forums.phpfreaks.com/topic/325232-normal-user-and-admin-user/#findComment-1638953 Share on other sites More sharing options...
jodunno Posted October 28 Share Posted October 28 Hello Mark2024, mac_gyver has offered a good solution for you. However, you should think about persistence and start using a session to carry information site-wide/across all of your pages. Otherwise, you would have to check a database per page load/reload. You can simply check the session variable instead of using a variable with local (this script only) scope. Also, i am not criticizing or trying to change your design. Design your site as you wish. However, the font tag is deprecated and we stopped using non-breaking spaces in empty td cells many, many years ago, along with hr and 1x1 pixel gifs (although, some shady advertisers still use the 1x1 pixel gifs, LOL). Try switching to css for styling your documents and let html structure the document. <?php session_start(); //$_SESSION['loggedin'] = 0; $_SESSION['level'] = 0; $_SESSION['loggedin'] = 1; $_SESSION['level'] = 1; //admin //$_SESSION['loggedin'] = 1; $_SESSION['level'] = 0; //regular user /* number values can be trivial at times. A string with a short value is usually better. $_SESSION['level'] = (string) 'admin'; $_SESSION['level'] = (string) 'patron'; et cetera then if(!empty($_SESSION['level']) && $_SESSION['level'] === 'admin') */ ?> <html> <head> <title></title> <style type="text/css"> h3.welcome { color: yellow; } </style> </head> <body> <div id="left"> <img src="images/john.png" height="100px" width="300px" /> </div> <div id="right"> <div id="view_box"> <ul> <li><img src="images/1.png" /></li> </ul> </div> <div id="button"> <ul> <!-- <li><button class="button" scroll_value="0">*</button></li> -- > <!-- <li><button class="button" scroll_value="600">*</button></li> --> <!-- <li><button class="button" scroll_value="1200">*</button></li> --> </ul> </div> <hr /> <?php if($_SESSION['loggedin']){ ?> <h3 class="welcome">Welcome, customer_name</h3> <h3>Money That Your Due Today</h3> <table border=1"> <tr><th>FULL NAME</th><th>PURCHASE DATE</th><th>MONEY DUE</th><th>UPDATE</th></tr> <tr> <td></td> <td></td> <td></td> <td></td> </tr> <td></td> <td></td> <td></td> <td></td> </tr> <tr> <td></td> <td>TOTAL DUE</td> <td>£ totmoney </td> <td></td> </tr> </table> <br /><br /><br /> <?php } else { ?> <h3>Login To Your Account</h3> <form method="post" action="login.php"> <table cellspacing="10" cellpadding="10"> <tr><td>Username</td><td><input type="text" name="username" /></td></tr> <tr><td>Password</td><td><input type="password" name="password" /></td></tr> <tr><td></td><td><input type="submit" name="submit" value="Login To Your Account" /></td></tr> </table> </form> <?php } ?> </div> <!-- <?php //include ("styles/bottom.php"); ?> --> <?php if(!empty($_SESSION['level']) && $_SESSION['level']){ echo "<a href='admin.php'>Admin Panel</a> |"; } /* or if !empty switch for multiple entries: //if(!empty($_SESSION['level'])) { //switch ($_SESSION['level']) { //case 'admin': echo "<a href='admin.php'>Admin Panel</a> |"; break; //case 'patron': echo "<a href='offers.php'>Special Offers</a> |"; break; //}} */ ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/325232-normal-user-and-admin-user/#findComment-1638979 Share on other sites More sharing options...
Moorcam Posted November 2 Share Posted November 2 I know a solution has been established, but just wanted to put my 2c in. Here is what I use to show content based on user roles: <?php $user_id = $_SESSION['user_id']; $stmt = $conn->prepare("SELECT role FROM users WHERE user_id = ?"); $stmt->bind_param("i", $user_id); $stmt->execute(); $stmt->bind_result($role); $stmt->fetch(); $stmt->close(); switch ($role) { case 'Admin': echo 'I am admin'; break; case 'Member': echo 'I am Member'; break; default: echo 'Whatever!'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/325232-normal-user-and-admin-user/#findComment-1639232 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.