Jump to content

RaiN3772

Members
  • Posts

    21
  • Joined

  • Last visited

RaiN3772's Achievements

Member

Member (2/5)

1

Reputation

  1. 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?
  2. 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
  3. 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.
  4. 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
  5. @gizmolathank you so much for making it clear, select form is more logical for this type and i ended up doing it. Thanks
  6. isn't there any possible way to do it with checkbox type? or a tweak with jquery?
  7. 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?
  8. 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
  9. 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
  10. <?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
  11. 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
  12. 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"
  13. 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
  14. didnt know such things existed lol, im newbie to php world, thanks alot mate
×
×
  • 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.