Jump to content

RaiN3772

Members
  • Posts

    23
  • Joined

  • Last visited

Everything posted by RaiN3772

  1. yh that is me. i was trying to join from the header button "Join our Discord!` not from the green bar in the top of the page.
  2. hello there, im not sure if this is the correct forum for this type of posts but i'm trying to join the discord server but the link is invalid or expired, can someone update it? Thanks
  3. im not even sure how to do that, i got this router from github as im still learning/experimenting, can you give me a code or something to make it work?
  4. Hello there, basically i have a problem accessing a variables so my files structure is - Project Root - app - inc - .htaccess - index.php index.php code is basically require_once __DIR__ . '/inc/core.php'; the core file inside inc folder is $test = 1; require_once 'inc/classes/router.php'; $route = new Route(); include 'routes.php'; the router class inside inc/classes/router.php is class Route { private function simpleRoute($file, $route){ if(!empty($_REQUEST['uri'])){ $route = preg_replace("/(^\/)|(\/$)/","",$route); $reqUri = preg_replace("/(^\/)|(\/$)/","",$_REQUEST['uri']); }else{ $reqUri = "/"; } if($reqUri == $route){ $params = []; include($file); exit(); } } function add($route,$file){ //will store all the parameters value in this array $params = []; //will store all the parameters names in this array $paramKey = []; //finding if there is any {?} parameter in $route preg_match_all("/(?<={).+?(?=})/", $route, $paramMatches); //if the route does not contain any param call simpleRoute(); if(empty($paramMatches[0])){ $this->simpleRoute($file,$route); return; } //setting parameters names foreach($paramMatches[0] as $key){ $paramKey[] = $key; } //replacing first and last slashes //$_REQUEST['uri'] will be empty if req uri is / if(!empty($_REQUEST['uri'])){ $route = preg_replace("/(^\/)|(\/$)/","",$route); $reqUri = preg_replace("/(^\/)|(\/$)/","",$_REQUEST['uri']); }else{ $reqUri = "/"; } //exploding route address $uri = explode("/", $route); //will store index number where {?} parameter is required in the $route $indexNum = []; //storing index number, where {?} parameter is required with the help of regex foreach($uri as $index => $param){ if(preg_match("/{.*}/", $param)){ $indexNum[] = $index; } } //exploding request uri string to array to get //the exact index number value of parameter from $_REQUEST['uri'] $reqUri = explode("/", $reqUri); //running for each loop to set the exact index number with reg expression //this will help in matching route foreach($indexNum as $key => $index){ //in case if req uri with param index is empty then return //because url is not valid for this route if(empty($reqUri[$index])){ return; } //setting params with params names $params[$paramKey[$key]] = $reqUri[$index]; //this is to create a regex for comparing route address $reqUri[$index] = "{.*}"; } //converting array to sting $reqUri = implode("/",$reqUri); //replace all / with \/ for reg expression //regex to match route is ready ! $reqUri = str_replace("/", '\\/', $reqUri); //now matching route with regex if(preg_match("/$reqUri/", $route)) { include($file); exit(); } } function notFound($file){ include($file); exit(); } } and the routes.php file inside the inc folder is $route->add("/", "app/index.php"); $route->add("/home","app/home.php"); $route->notFound("app/404.php"); and the index.php file inside the app folder is simple echo $test; but all i get is Undefined variable $test so literally i tried everything i know of for 2 days to get around this but i absolutely have no idea, i even tried to draw the file structure to see if they all are in the same page (i think so) but no idea. can someone point me to what is going on and why? thanks in advanced
  5. so what are you saying that i should create an ini or json file and when i want to edit some settings i should just use open/write? there is no reason for the database, i just dont want messy code/more files, just looking for the best practical coding solution.
  6. hello there, i want to fetch settings from database and use them in most of the files in the project, e.g website_name i would like to get it from the database and put it in the <title> tag. why i want to use the database instead of the php or ini file is so i can edit the settings from the web and not the file. i already know how to fetch the data from the database and use it, but my question is what is the best practice to do so. should i fetch all the data at once and use foreach loop in the core file of the project or should i use a class or something, im really lost. because most apps/forum use database settings such as mybb for example where u can edit the settings directly from the app but i dont know how to do it in a class, if someone could show me an example code i would really appreciate it. Thank you
  7. @gizmolathank you so much for making it clear, select form is more logical for this type and i ended up doing it. Thanks
  8. isn't there any possible way to do it with checkbox type? or a tweak with jquery?
  9. i ended up doing this, so far checking to dark is working <?php if(!isset($_COOKIE['mode'])) { $mode_check_flag = NULL; $mode = NULL; setcookie("mode", "light"); } if (isset($_COOKIE['mode'])) { $mode = $_COOKIE['mode']; echo "Cookie: " . $_COOKIE['mode'] . "<br>"; } if (isset($_COOKIE['mode']) && $_COOKIE['mode'] == "dark") { $mode_check_flag = "checked"; } else { $mode_check_flag = NULL; } if (!empty($_POST['mode']) && $_POST['mode'] == "light" ) { $mode = "dark"; $mode_check_flag = "checked"; setcookie("mode", $mode); } if (!empty($_POST['mode']) && $_POST['mode'] == "dark" ) { $mode = "light"; $mode_check_flag = NULL; setcookie("mode", $mode); } echo "Mode: " . $mode . "<br><br>"; ?> <html> <head> <title>Testing</title> </head> <body> <form action="" method="post"> <input type="checkbox" onChange="this.form.submit()" value="<?=$mode;?>" name="mode" <?=$mode_check_flag; ?> /> Dark Mode<br><br> </form> </body> </html> but when i uncheck the checked it doesnt change to light which is the last if statement, anyone knows why?
  10. the problem, that it doesn't work at all, lets say the box is checked (current dark), and i want to change it (uncheck to light) there is no possible way i know to check if the checkbox is being unchecked
  11. Hey there, im trying to create two stylesheets for my project, the first one is light and the second is dark, so basically i have this input checkbox type, so i want to display dark stylesheet if the button is checked and light if the button is unchecked basically my function <?php if(!isset($_COOKIE['mode'])) { $mode = "light"; } if (!empty($_POST['mode']) && $_POST['mode'] == "dark" ) { $mode = "dark"; setcookie("mode", "dark"); } else { $mode = "light"; setcookie("mode", "light"); } ?> and in my header <?php include 'inc/functions/cookies.php'; if($_COOKIE['mode'] == "dark") { echo "<link href='dark.css' rel='stylesheet' type='text/css' />"; } if($_COOKIE['mode'] == "light") { echo "<link href='light.css' rel='stylesheet' type='text/css' />"; } else { echo "<link href='light.css' rel='stylesheet' type='text/css' />"; } ?> and the html input checkbox is <input type="checkbox" onChange="this.form.submit()" value="<?=$mode;?>" name="mode" <?=$_COOKIE['mode'] == "dark" ? "checked" : "" ?> /> i might be overthinking this or something because i cannot figure it out, thanks in advance
  12. <?php // Include Database Configuration File require_once "inc/config.php"; try { // Select Statement // $sql = "SELECT * FROM items WHERE item_status = 'Approved'"; $sql = "SELECT * FROM items inner Join users ON items.uid = users.uid WHERE item_status = 'Approved'"; $statement = $pdo->query($sql); // Get all items as associative array $items = $statement->fetchAll(PDO::FETCH_ASSOC); } catch (PDOException $e) { echo "Error: " . $e->getMessage(); } $pdo = null; //close PDO connection function index_getItems() { global $items; foreach ($items as $items) { echo "<div class='d-flex align-items-sm-center mb-7'>"; echo "<div class='symbol symbol-60px symbol-2by3 me-4'>"; echo "<div class='symbol-label' style='background-image: url(" . $items["item_thumbnail"] . ")'></div>"; echo "</div>"; echo "<div class='d-flex flex-row-fluid flex-wrap align-items-center'>"; echo "<div class='flex-grow-1 me-2'>"; echo "<a href='#' class='text-gray-800 fw-bolder text-hover-primary fs-6'>" . $items["item_name"] . "</a>"; echo "<span class='text-muted fw-bold d-block pt-1'>" . $items["username"] . "</span>"; echo "</div>"; echo "<span class='badge badge-light-success fs-8 fw-bolder my-2'>$" . $items["item_price"] . "</span>"; echo "</div>"; echo "</div>"; } } ?> already did that to get all items, now i want to create page for item details, and pretty that the practical way to do so is not by creating each page for each item, "item-1.php" this way is efficient for alot of records, so i want a way to create a single page, and show item details depends on the $_GET method, so basically, if i go to item-1.php it should show me all the details on item ID = 1, same as 2 if go to item-2, it should show me the details about item 2...etc that is the question if you get what i'm talking about
  13. simple, one file multiple pages, example "website.com/page.php?id=1", real life example "youtube.com/watch?v=610XM39EE1o" where the page is v and the id is the video id, how do i do this, thats the question
  14. Hey @ginerjm, yes i already know html, css, long time experience with it actually, but lately i been learning php, so far, i already created listing all items as cards (bootstrap cards) from the database, also submitting a new item (insert into database) but i want to create a page where i can see item details, but i dont think the right method is to create each page for each item, so thats why im asking here, i think it has to do with $_GET or something and the url should be "view.php?=item-$item_id"
  15. hello there, im not sure how to explain this, also tried googling it, but nothing shows up. so basically im searching for a method or a function, lets say i have multiple items in the database (item id, item_name, item description, item_price...etc) so basically i want to create a page lets say its called "view.php" basically i'll put everything in it, connected it to the database etc, and display an item depends on the item id, so the url could be "view.php?item-id" or "view/item-id" or any other method that is available, if someone could refer me to something. not sure. thanks alot
  16. didnt know such things existed lol, im newbie to php world, thanks alot mate
  17. Hello there, i'm looking for a way to create a template system, i tried googling but nothing fancy shows up so my idea is to create a function where i can list the template name and its path in an array which should looks like this <?php function template($title) { $template = array( 'header' => include('inc/templates/header.php'), 'footer' => include('inc/templates/footer.php'), ); return $template[$title]; } ?> and in my index page i just call it with variable like <?=template('header');?> or {$header}, whatever works, but it seems like its duplicating my file contents, and some weird integer number at the bottom of the page, so is there an easy way that i can do or a tweak in my code? thanks
  18. make sense, thanks alot for the help
  19. i ended up doing this, thank you so much for your help the final code is: $employees = array ( array('name' => 'Employee-1', 'department' => 'IT', 'salary' => 5000), array('name' => 'Employee-2', 'department' => 'HR', 'salary' => 4000), array('name' => 'Employee-3', 'department' => 'Marketing', 'salary' => 3000), array('name' => 'Employee-4', 'department' => 'Sales', 'salary' => 1950), array('name' => 'Employee-5', 'department' => 'Management', 'salary' => 1700), array('name' => 'Employee-6', 'department' => 'Finance', 'salary' => 1500) ); function getTotalSalaries() { $totalSalaries = 0; global $employees; // We want to use a global variable from outside the function foreach ($employees as $value) { $totalSalaries += $value['salary']; } return $totalSalaries; } function getMaxSalary() { global $employees; $maxSalary = max(array_column($employees, 'salary')); // Get the maximum value from the returned values in a single column (salary) in the input array return $maxSalary; } Results: https://imgur.com/GAxkzzE
  20. Hello there, i have this class assignment which im struggling with, im not even sure of the assignment is correct, so basically the assignment is: https://prnt.sc/1xidzwc im struggling with associative array, (still im pretty sure we have to use multidimensional array) and foreach, how im supposed to dislpay it so here is what i ended up with <?php /* $employees = array ( array("Employee-1", "IT", 5000), array("Employee-2", "HR", 4000), array("Employee-3", "Marketing", 3000), array("Employee-4", "Sales", 1500), array("Employee-5", "Somewhere", 4600), array("Employee-6", "Here", 3700) ); */ $name = array("Employee-1", "Employee-2", "Employee-3", "Employee-4", "Employee-5", "Employee-6"); $department = array("IT", "Sales", "Marketing", "HR", "Here", "There"); $salary = array(5000, 4000, 4500, 3000, 1500, 3600); $table = array("Employee Name" => $name, "Department" => $department, "Salary"=>$salary); ?> <table class="table"> <thead> <tr> <th>Employee Name</th> <th>Department</th> <th>Salary</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>2</td> <td>3</td> </tr> </tbody> </table> so what i tried is to use foreach and nested loop foreach ($table as $k => $v) { echo "<tr>"; for ($a=0; $a < count($v); $a++){ echo "<td>" . $v[$a] . "</td>"; } echo "</tr>"; } but it displays the output as rows not the names in columns and the info in the other side, second even if it works still its not what the professor asked for, is there anything im missing here?
  21. i gave up... im not sure what exactly is the problem. it was working fine without any error till i organized the files, all i did created a new folder called inc, then created a file called db_config.php which is the normal mysql connection (nothing feaky lol) <?php // Database configuration $db_host = 'localhost'; $db_username = 'root'; $db_password = ''; $db_name = 'somename'; ?> then i included the file function_login.php in index.php (which is the login page) and changed the form action from function_login.php to index.php nothing fancy you know. but what i think is going on here that the functions is working before i submitting the form (which it was working perfectly before i organized the files)
  22. Hello there, totally new to php and been learning it in few weeks, and im in love with its potential. so im here to get some help creating a login function. what im getting is "Please fill both the username and password fields" which the code is "exit ("Please fill both the username and password fields");" what im supposed to get is a normal html form to submit the username and password here is the html code <?php include 'inc/function_login.php'; ?> <html> <head> <meta charset="utf-8"> <title>Login</title> </head> <body> <div class="login"> <h1>Login</h1> <form action="index.php" method="post"> <input type="text" name="username" placeholder="Username" id="username" required> <input type="password" name="password" placeholder="Password" id="password" required> <input type="submit" value="Login"> </form> </div> </body> </html> and here is the php code (i know you dont wanna read 100k lines of codes that will probably gonna give you a headache but im helpless at this point, so thanks) <?php // Initialize the session session_start(); // Include Database Configuration File require_once "db_config.php"; // Include Langauge File require_once "language.php"; // Attempt to connect to the database server $con = mysqli_connect($db_host, $db_username, $db_password, $db_name); if ( mysqli_connect_errno() ) { // If there is an error with the connection, stop the script and display the error. exit('Failed to connect to MySQL: ' . mysqli_connect_error()); } // Check if the data from the login form was submitted, isset() will check if the data exists. if ( !isset($_POST['username'], $_POST['password']) ) { // Could not get the data that should have been sent. exit ("Please fill both the username and password fields"); } // Prepare our SQL, preparing the SQL statement will prevent SQL injection. if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) { // Bind parameters (s = string, i = int, b = blob, etc), in our case the username is a string so we use "s" $stmt->bind_param('s', $_POST['username']); $stmt->execute(); // Store the result so we can check if the account exists in the database $stmt->store_result(); if ($stmt->num_rows > 0) { $stmt->bind_result($id, $password); $stmt->fetch(); // Account exists cool, Please verify the password. if (password_verify($_POST['password'], $password)) { // Verification success! User has logged-in! // Create sessions, so we know the user is logged in session_regenerate_id(); $_SESSION['loggedin'] = TRUE; $_SESSION['name'] = $_POST['username']; $_SESSION['id'] = $id; header('Location: home.php'); } else { // Incorrect password echo 'Incorrect username and/or password!'; } } else { // Incorrect username echo 'Incorrect username and/or password!'; } $stmt->close(); } ?> i would be glad if someone point me to the right way, or at least tell me what is totally wrong
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.