-
Posts
293 -
Joined
-
Last visited
-
Days Won
5
Everything posted by jodunno
-
0.0 empty works. see my included code and use it in xampp. if you open an anchor tag outside of php, then close the anchor tag outside of php. because apparently the user variable is undefined and the ending anchor tag is contained within illogical php code that is not executing. <?php //$undefinedVariable = '!empty'; //$user = ''; $user = 'test'; $userhref = $user ? 'href="https://www.example.com/mycp.php"': 'href="https://www.example.com/login.php"'; $usertext = $user ? $user: 'Member Login'; ?> <ul><li class="li-btn"> <a class="custom-btn custom-btn--small custom-btn--style-4" <?php if (empty($user)){ echo 'href="https://www.example.com/login.php">Member Login'; } else { echo 'href="https://www.example.com/mycp.php">'.$user; } ?> </a> </li></ul> <ul><li class="li-btn"> <a class="custom-btn custom-btn--small custom-btn--style-4" <?php echo $userhref . '>' . $usertext; ?></a> </li></ul> <ul>Also: <li class="li-btn"> <?php echo empty($undefinedVariable) ? 'empty == true': $undefinedVariable; ?> </li> </ul> start over and think about what you are trying to accomplish. for one thing, user must mean that you have a login. How are you storing user on every page within your site? is user variable based upon a form input (post)? then one has to login on every page? one would have to login on every different page because a variable is local to the current script. You should be using a session variable or reading user from a database. Show us your code for user variable.
-
try empty(), which will supress warnings if a variable does not exist: <?php <a class="custom-btn custom-btn--small custom-btn--style-4" if (empty($user)){ echo 'href="https://www.paratuberculosis.com/login.php">Member Login</a>>'; } ?> https://www.php.net/manual/en/function.empty.php it will handle '', 0, !isset and false I assume that you are using wordpress or some other pre-coded resource You should be in control of your variables, especially user-based variables.
-
well it bothers me that you think that a full uri is a problem, so i revisit your code and voila! you have output before the header, which is an error. You must use header before any output is sent. I apologize for not noticing this error beforehand. I overlooked it yesterday. So perhaps that was your problem all along. Meantime, i have to work on reading and comprehension skills. I missed that yesterday. yikes!
-
Danke schön. Sehr nett, aber: a full uri should not be a problem. <?php session_start(); if (is_int(intval($_POST["PQty1"]))) { $_SESSION["PQty1"] = $_POST["PQty1"]; } header("Location: http://localhost/p5k/form.php"); exit; ?> you could also submit to the same page: <?php session_start(); print '<html><body> <form method="POST"> <select name="PQty1"> <option value="">Please select quantity</option>'; for ($i=1;$i<=20;$i++) { print '<option value="'.$i.'">'.$i.'</option>'; } print '</select><input type="submit" value="Add"></form>'; switch ($_SERVER['REQUEST_METHOD']) { case 'POST': if (is_int(intval($_POST["PQty1"]))) { $_SESSION["PQty1"] = $_POST["PQty1"]; } if (!empty($_SESSION["PQty1"])) { print '<p>'.$_SESSION["PQty1"].'</p>'; } break; } print '</body></html>'; ?> still, the general rule for redirection is to follow the redirection with exit. Otherwise, the script will continue executing to the end of the script. Not good if you have data after the redirection, which should not be processed. If you are happy and the script is working, then i am happy for you. I hope that you have a pleasant day, John
-
well i have changed a few things in your script which irritate me but the majority of the code is unchanged. I tested your code in xampp and i do not have any issues with the session. I have to enable cookies in Firefox or the session will not function but everything is working as expected after i enable cookies. <?php session_start(); print "<html><body> <form method=POST action=Cart1.php> <select name=PQty1> <option value=>Please select quantity</option>"; for ($i=1;$i<=20;$i++) { print "<option value=$i>$i</option>"; } print "</select><input type=submit value=Add></form>"; if (!empty($_SESSION["PQty1"])) { print "<p>".$_SESSION["PQty1"]."</p>"; } print "</body></html>"; ?> <?php session_start(); if (is_int(intval($_POST["PQty1"]))) { $_SESSION["PQty1"] = $_POST["PQty1"]; } header("Location: /p5k/form.php"); exit; ?> you have to check the local storage to see if your session cookie matches that which is sent by the server.
-
form.php start tag should be <?php you are assigning user input directly to a session variable without validating the input. I recommend casting to an integer intval() and using is_int to verify a number. or die(); will cause an error. simply exit after a header redirection: header("Location: form.php"); exit; if session cookies are not being set in the browser, then you will have empty session files (the browser will continue starting a new session). Which brings you back to the suggestions posted by requinix.
-
okay but now you need to take a closer look at your code. Specifically, not equal to. if ($_SESSION["AdmKeyy"] !== "T54") { } it seems as though your sessions may not be coded properly. Requinix is a pro and will easily be able to spot any problems. Perhaps you could post a full example of your session code.
-
and hopefully you also have session_start(); before that line of code on every individual page that needs to continue the previously started session. we cannot see your code, so we are feeling around in the dark for a flashlight...
-
I do not see a reference to a book. I find the answer to be sufficient for trouble shooting. And i will add to this: https://firefox-source-docs.mozilla.org/devtools-user/ https://developer.chrome.com/docs/devtools https://learn.microsoft.com/en-us/microsoft-edge/devtools-guide-chromium/landing/ https://developer.apple.com/library/archive/documentation/AppleApplications/Conceptual/Safari_Developer_Guide/Introduction/Introduction.html https://www.php.net/manual/en/book.session.php browser issues: disabling all cookies will include session cookies in certain browsers. clear your cache before testing. check php session file permissions, typical path is /var/lib/php/sessions check php.ini for PHP session path location: /etc/php.ini search for session.save_path in php.ini check PHP error logs: query a search engine for error log locations of your preferred web server. I also wonder what you mean by "Ubuntu only". So this problem doesn't happen in OpenSUSE or Linux Mint? or you mean that the problem is not with a Microsoft Windows system, rather a Linux based system like Ubuntu?
-
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>
-
$_SERVER['DOCUMENT_ROOT']; = server get the root path of this document which really is C:/xampp/htdocs/ switch to using root-based paths (/). require_once dirname(__FILE__) . '/dir/file.php'; / = start at root (htdocs) dir/ = enter folder named dir file.php = load this file echo '<link rel="Stylesheet" type="text/css" href="/css/styles.css" />'; / = start at root (htdocs) css/ = enter folder named css styles.css = get this file require_once dirname(__FILE__) . '/../dir/file.php'; / = start at root (htdocs) ../ = move up one dir (now outside of root) dir/ = enter folder named dir file.php = load this file
-
how to calculate a certain number of times a fraction?
jodunno replied to NikitosKnyaz's topic in PHP Coding Help
an html table is not a good idea if n > 16. Even lists surrender to the spatial demands. Here is my take on the matter. I work off of the old saying, "2 plus 2 is 4". <?php $n = (int) 5; /* $_POST['n'] from <input type="number" name="n"> */ $fourSure = function($x) { $i = $j = (int) 2; $e = array(); $e['d'] = $e['n'] = array(); for (; $x > 0; $x--) { for (; $i < $x; $i+=2, $j+=4) { $e['n'][] = $j >> 1; $e['d'][] = $i; }} return join('·', $e['n']) . ' ÷ ' . join('·', $e['d']); }; $n = $n*2+2; echo '<ul style="font-family: sans-serif; list-style-type: none;">S = '; for ($i=4; $i <= $n; $i+=2) { echo '<li>'.$fourSure($i).'</li>'; } echo '</ul>'; ?> to each his own. -
For Your Information and speaking of arrogance: "Can you solve 4 queens problem without using 2d arrays, recursion/backtracking? Just display 1 such solution." 4 queens not 8. which simply involves the 40deg connected vertices of the perimiters, which offers two immediate solutions. the pythagorean theorem affords your consciousness a visual of the process, so that you can better understand it. I guess it doesn't help. recursion and backtracking is silly and time consuming. But if you want to discuss an 8x8 matrix, then: Using the left/west side of the central 40deg diagonal as an example: we can create a (10 lines of code) loop to gather all of the diagonals and 90deg right triangles (while we're at it). attached image. However, i already have 'diagonals' stored in a vertex adjacency of ordinal directions (neighbor) array. One could easily check NW vertex, thus NW vertex loop to the N/S border. Clearing rows and columns requires no further code (if one has constructed a grid graph properly). I drink coffee.
-
Hello? you disappoint me. The code that i posted must be far over your heads. Two solutions are revealed in the code but two people here are unable to see it. LOL. Pythagorean theorem reveals diagonals (id est right angle triangles), which will place the queens in different diagonals, LOL, in different rows and columns. LOL. good lord! I feel like i'm talking to a wall. Nevermind. The concept is beyond the both of you. Carry on.... 🙂
-
i do not use Mailjet but i read an article and the author suggests that your email is to be placed in the From array and the post email in the To array. It doesn't make sense to me but the author of the article uses this method in his form submission code.
-
I do not use Mailjet but i have discovered that other users have had a similar problem with the api. try specifying the api $mj = new \Mailjet\Client($apikey, $apisecret, true,['version' => 'v3.1']); otherwise, i have to run a setup myself to see if i can make it work.
-
one mark (queen if you want to play chess) per row per column is not rocket science and not an exercise that is worthy of programming exercise. add vision to your code and logic. If you asked me to find, quote, one solution, then let us prey: <?php for ($i = 1; $i <= 4; $i++) { echo '<div>'; for ($j = 1; $j <= 4; $j++) { echo '<div style="display: inline-block; padding: 8px; width: 40px;">' . pow($i,2) + pow($j,2) . '</div>'; } echo '</div>'; } //instructions: find the matching numbers around the perimiters //example: find the 5 and opposite 10. find the 25 and the 20 //queens are set one per column per row. finished. what? ?>
-
no arrays, recursion/backtracking? I had to research what is meant by "4 queens problem". I am not a chess player and i'm a self taught coder. If i am understanding the rules correctly: one per diagonal per row. m4 x n4 and four queens. I would convert the vertices using pythagorean theorem to find the diagonals. Place your marks and store the row and column in a variable. It would be much faster than recursion and arrays anyway. One solution is what you mentioned.
-
1. unauthorized error = You have specified an incorrect Api Key / API Secret Key. 2. To use the Mailjet Email API, you need to: Create a Mailjet account, then retrieve your API and Secret keys. They will be used for authentication purposes. have you created an account? just trying to clarify. if you have valid api credentials, then Initialize your Mailjet Client: https://github.com/mailjet/mailjet-apiv3-php?tab=readme-ov-file#authentication
-
how to calculate a certain number of times a fraction?
jodunno replied to NikitosKnyaz's topic in PHP Coding Help
you, Sir, have a wonderful sense of humour. I like you 🙂 -
how to calculate a certain number of times a fraction?
jodunno replied to NikitosKnyaz's topic in PHP Coding Help
you're supposed to recreate a paper with fake math problems? well, why don't you just scan it? maybe we can use html and svg to recreate the paper. You'll have to draw the svg arrows yourself, i have no time for that or the squiggly scribble. <html> <head> <title>paper tigers</title> <style> body { font-family: "Helvetica", "Open Sans", "Arial Narrow", sans-serif; } span.circled { padding: 1px; border: solid 1px #000000; border-radius: 50%; } div.textForm { display: inline-block; position: absolute; top: 3em; left: 16em; width: 8em; height: auto; } .invisibleInput { margin: 2px; width: 6em; height: 1.2em; border: solid 4px #000000; border-radius: 30%; } .htmlform { display: inline-block; text-indent: 1em; } .submit { display: inline-block; position: absolute; left: 2em; width: 3.5em; padding: 4px; border: solid 4px #000000; } .babold { font-weight: bold; } .parity { margin-left: 13em; font-family: "Arial Narrow", sans-serif; font-size: 0.9em; } .penned { font-family: "Comic Sans MS", "Open Sans", "Arial", sans-serif; color: #5c4ea1; } th, td { padding: 0px 3px; font-family: "Comic Sans MS", "Open Sans", "Arial", sans-serif; color: #5c4ea1; } .divider { margin: 0px; padding: 0px; width: 100%; height: 1px; background: #5c4ea1; } .th { font-weight: normal; } </style> </head> <body> <?php echo '<div class="textForm"><div class="babold htmlform">html form</div>'.PHP_EOL; echo '<div class="invisibleInput"></div>'.PHP_EOL; echo '<div class="babold submit">submit</div></div>'.PHP_EOL; $n = 3; $nm = $n-1; function numerator(int $n = 0, int $circled = 0) { if ($n === 1) { return 1; } $numerator = array (); $n*= 2; $nm = $n-1; for ($i = 1; $i < $n; $i+=2) { if ($circled && $i===$nm) { $numerator[] = '<span class="circled">'.$i.'</span>'; continue; } $numerator[] = $i; } return join('.', $numerator); } function denominator($n) { if ($n === 1) { return 2; } $denominator = array (); $n*= 2; for ($i = 2; $i <= $n; $i+=2) { $denominator[] = $i; } return join('.', $denominator); } echo '<p class="babold">limit fraction</p>'.PHP_EOL; echo '<p class="penned">N = 3</p>'.PHP_EOL; /* $_POST['n'] = 3 ///////////////////////////////////////////////////////////////////////*/ $numerators = array(); $denominators = array(); for ($i = 1; $i <= $n; $i++) { $numerators[] = numerator($i, 0); $denominators[] = denominator($i); } echo '<table>'; echo '<tbody>'; echo '<tr>'; echo '<td>S = </td>'; for ($i = 0; $i < $n; $i++) { echo '<td><table><tr><td>' . $numerators[$i] . '</td></tr><tr><td class="divider"></td></tr>' . '<tr><td>' . $denominators[$i] . '</td></tr></table>' . (($i===$nm) ? '</td><td>=</td>':'</td><td>+</td>').PHP_EOL; } echo '</tr>'; echo '</tbody></table>'.PHP_EOL; echo '<span class="babold parity">in each new fraction the number increases by "2".</span>'.PHP_EOL; /* $_POST['n'] = 4 ///////////////////////////////////////////////////////////////////////*/ $n = 4; $nm = $n-1; $numerators = array(); $denominators = array(); for ($i = 1; $i <= $n; $i++) { $numerators[] = numerator($i, 1); $denominators[] = denominator($i); } echo '<table>'; echo '<tbody><thead><th class="th">N = 4</th></thead>'; echo '<tr>'; echo '<td>S = </td>'; for ($i = 0; $i < $n; $i++) { echo '<td><table><tr><td>' . $numerators[$i] . '</td></tr><tr><td class="divider"></td></tr>' . '<tr><td>' . $denominators[$i] . '</td></tr></table>' . (($i===$nm) ? '':'</td><td>+</td>').PHP_EOL; } echo '</tr>'; echo '</tbody></table>'.PHP_EOL; /* $_POST['n'] = 5 ///////////////////////////////////////////////////////////////////////*/ $n = 5; $nm = $n-1; $numerators = array(); $denominators = array(); for ($i = 1; $i <= $n; $i++) { $numerators[] = numerator($i, 0); $denominators[] = denominator($i); } echo '<table>'; echo '<tbody><thead><th class="th">N = 5</th></thead>'; echo '<tr>'; echo '<td>S = </td>'; for ($i = 0; $i < $n; $i++) { echo '<td><table><tr><td>' . $numerators[$i] . '</td></tr><tr><td class="divider"></td></tr>' . '<tr><td>' . $denominators[$i] . '</td></tr></table>' . (($i===$nm) ? '':'</td><td>+</td>').PHP_EOL; } echo '</tr>'; echo '</tbody></table>'.PHP_EOL; ?> </body></html> and, honestly, this is the worst code that i could think of. I hope that you fail 🙂 cheating gets you nowehere, unless you're bodybuilding. The Arnold cheat is quite good. I gained 2 inches using the Arnold cheat. -
honestly, whenever you have a wealth of functions doing their deeds, essentially you have the makings of a class. Frameworks are class based anyway, so it would be wise for one to make the switch to classes or frameworks. Furthermore, the database problems that you have mentioned can be mitigated by initial design, which should take these matters into account when setting up such a system. Think, plan, design, double-check, think more, imagine scenarios you haven't yet thought about, then build a development version for testing. If you build a solid foundation, then the site/project will withstand various environmental changes. Otherwise, the site/project will collapse like a stick house in a collision with an f5 tornado. One could go out-of-business with a bad design. Design is probably a scientific study on its own. The subject of architecture is intense. You should follow the advice by the pros in this forum. Alot of prominent members are are, in fact, pros and they have been through the bad times as well as the good times. The pros here can save you alot of time and hassle. If it is recommended to use a framework, then you should probably start reading about the suggested framework(-s).
-
Finally, i got around to completing my cycle breaking code, which circumvents a path finding algorithm already provided by Barand. My code is raw development code so don't fall out of your chairs while viewing the mess. LOL. <?php /*/////////////////////////////////////////////////////////////////////////////////////////////////*/ /*// WARNING! raw and rough development code lies ahead. //*/ /*// m = row, n = column, mp np = mn parity, p = perimiter, t = tile number, //*/ /*// N = north, W = west, S = south, E = east and subsequently NW NE SW SE //*/ /*// cv = central vertices, cy = cycles, cr = crossroads or intersections, //*/ /*// cyan = cycle analysis, q = quadratic or square matrix, a = matrix entry ai,j //*/ /*// omidirectional paths N,W,S,E = crossroads (four-ways or "circles") //*/ /*// development information: //*/ /*// made in Germany //*/ /*// my vlc repeat playlist: Sabrina Carpenter - Espresso and Glorilla - Yeah, Glo! //*/ /*// author: Sherkosky, John //*/ /*/////////////////////////////////////////////////////////////////////////////////////////////////*/ $ac = array (); /* accounting array. you can count on it! */ $ac['cr'] = $ac['cv'] = $ac['q'] = $ac['n-1'] = $ac['n+1'] = $ac['m-1'] = $ac['m+1'] = $ac['np'] = $ac['mp'] = $ac['v'] = $ac['n'] = $ac['m'] = (int) 0; $cyan = $ac['p'] = $ac['e'] = $cv = array (); $cyan['cy'] = array (); $cyan['cr'] = array (); $ac['e']['vn'] = $ac['e']['hm'] = $ac['e']['s'] = $ac['e']['z'] = $ac['e']['y'] = $ac['e']['x'] = (int) 0; $algor = array(); $algor['cb'] = 1; $algor['pf'] = 0; /* obviously, post inclusion is development code only. validation and restrictions withheld in addition to this fact, and most obvious in code, the form has since been removed. */ empty($_POST['m']) ? $ac['m'] = (int) 3: $ac['m'] = (int) $_POST['m']; empty($_POST['n']) ? $ac['n'] = (int) 3: $ac['n'] = (int) $_POST['n']; $ac['v'] = $ac['m'] * $ac['n']; if ($ac['m'] === $ac['n']) { $ac['q'] = 1; } /* quadratic or square matrix */ $p = array(); $p['c'] = $p['E'] = $p['S'] = $p['W'] = $p['N'] = $p['u'] = array(); /* initialize grid graph arrays. be smart. */ $init_gg = function(int $ab = 0, int $cd = 0, int $i = 0, int $m = 0, int $n = 0, int $v = 0) { $gg = array(); switch ($ab) { case 0: /* row and column index for tiles (an address book or whois function) */ for ($j = 1; $j <= $m; $j++) { for ($k = 1; $k <= $n; $k++) { $gg[$j][$k]['t'] = $i; $i++; }} break; case 1: /* (0) initialize main array organized by tile numbers $t. (1) cardinal directions. (2) ordinal directions. */ for ($i; $i <= $v; $i++) { switch ($cd) { case 0: $gg[$i] = Array('xy' => Array('m' => 0, 'n' => 0, 'a' => 0, 'mn' => '', 'dp' => $i % 2 !== 0 ? 0: 1), 'a*' => Array('g' => 0, 'h' => 0, 'f' => 0 )); break; case 1: $gg[$i]['N'] = 0; $gg[$i]['W'] = 0; $gg[$i]['S'] = 0; $gg[$i]['E'] = 0; break; case 2: $gg[$i]['NW'] = 0; $gg[$i]['NE'] = 0; $gg[$i]['SW'] = 0; $gg[$i]['SE'] = 0; break; }} break; } return $gg; }; $gg = (array) $init_gg(0, 0, 1, $ac['m'], $ac['n'], $ac['v']); $ggt = (array) $init_gg(1, 0, 1, 0, 0, $ac['v']); $ggcd = (array) $init_gg(1, 1, 1, 0, 0, $ac['v']); $ggod = (array) $init_gg(1, 2, 1, 0, 0, $ac['v']); $gge = array (); /* gg edges: hm = horizontal m or x. vn = vertical n or y */ $gge['hm'] = array (); $gge['vn'] = array (); $gge['c'] = (int) 0; /* debugging: count of cycle disconnections */ $ggDisplay = (int) 1; /* development variable for viewing gg data */ $nwse = $k = $w = $e = $a = $r = $t = $j = $i = (int) 0; $nodes = array(); $nodes['s']['n'] = $nodes['s']['m'] = $nodes['s']['t'] = (int) 0; $nodes['g']['n'] = $nodes['g']['m'] = $nodes['g']['t'] = (int) 0; $nodes['s']['i'] = $nodes['s']['mn'] = (string) ''; $nodes['g']['i'] = $nodes['g']['mn'] = (string) ''; $nodes['n']['d'] = $nodes['m']['d'] = null; /* svg tiles. later to be if (svg), since i am building a canvas version */ $svg = array(); $svg['mx'] = $svg['sy'] = $svg['sx'] = $svg['ry'] = $svg['rx'] = (int) 0; $svg['rf'] = (string) 'bg101010'; $svg['ry'] = $svg['rx'] = 10; $svg['e'] = array(); $svg['e']['vy'] = $svg['e']['vx'] = $svg['e']['hy'] = $svg['e']['hx'] = (int) 0; $svg['e']['hx'] = 80; $svg['e']['hy'] = 90; $svg['e']['vx'] = 30; $svg['e']['vy'] = 40; /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ /* functions */ /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ function calculate_center($m, $n, $v, $mparity, $nparity) { /* mt = medial tiles, d = direction, t = tiles */ $mt = (array) ['d' => 'center', 't' => (array) []]; $half = floor(($v + 1) / 2); $halves = $half + 1; $t1 = $half - floor($half / $m); $t2 = $t1 + 1; $t3 = $t1 + $n; $t4 = $t3 + 1; switch ($nparity) { case 0: if ($mparity) { $mt['d'] ='vertical'; $mt['t'][$t1] = $t1; $mt['t'][$t3] = $t3; break; } $mt['t'][$half] = $half; break; case 1: if ($mparity) { $mt['t'][$t1] = $t1; $mt['t'][$t2] = $t2; $mt['t'][$t3] = $t3; $mt['t'][$t4] = $t4; break; } $mt['d'] ='horizontal'; $mt['t'][$half] = $half; $mt['t'][$halves] = $halves; break; } return (array) $mt; } function manhattan_distance(int $x1 = 0, int $x2 = 0, int $y1 = 0, int $y2 = 0) { return abs($x1-$x2) + abs($y1-$y2); /* h(n) = |xn - xg| + |yn - yg| */ } function perimeters(int $m = 0, int $n = 0, int $v = 0, string $d = '', int $u = 0) { /* u=unique = faster and easier than using array_unique(array_merge()) on cardinal points */ /* several methods of calculation exist. we picked one and loop. a loop is not necessary. */ $perimeters = (array) []; switch ($u) { case 0: switch (strtoupper($d)) { case 'N': for ($i = 1; $i <= $n; $i++) { $perimeters[$i] = $i; } break; case 'W': for ($i = 1; $i <= $v; $i+=$n) { $perimeters[$i] = $i; } break; case 'S': for ($i = $v - ($n - 1); $i <= $v; $i++) { $perimeters[$i] = $i; } break; case 'E': for ($i = $n; $i <= $v; $i+=$n) { $perimeters[$i] = $i; } break; } break; case 1: for ($i = 1; $i <= $n; $i++) { array_push($perimeters, $i); } for ($i = 1+$n; $i < $v-($n - 1); $i+=$n) { array_push($perimeters, $i); } for ($i = $v - ($n - 1); $i <= $v; $i++) { array_push($perimeters, $i); } for ($i = $n*2; $i < $v; $i+=$n) { array_push($perimeters, $i); } sort($perimeters); break; } return $perimeters; } function randomize(int $min = 0, int $max = 0, string $fn = '') { /* allows user to select from multiple methods */ $valid = (array) ['random_int', 'mt_rand', 'rand']; if (!in_array($fn, $valid) === true) { return false; } return $fn($min, $max); } function cbr(array $cy = [], array $e = [], callable $randomize){ $i = (int) 1; $edges = array (); $re = (int) 0; $hve = array (); $hve['t'] = $hve['d'] = 0; foreach ($cy as $t => $s) { $edges[$i] = $t; $i++; } $re = array_rand($edges, 1); $hve['t'] = $edges[$re]; switch ($re) { case 1: $re = 0; break; case 2: $re = 2; break; case 3: $re = 1; break; } switch ($re) { case 0: if (empty($e['hm'][$hve['t']]) && empty($e['vn'][$hve['t']])) { switch (randomize(1, 2, 'random_int')) { case 1: $hve['d'] = 'hm'; break(2); case 2: $hve['d'] = 'vn'; break(2); } } empty($e['hm'][$hve['t']]) ? $hve['d'] = 'hm': $hve['d'] = 'vn'; break; case 1: $hve['d'] = 'hm'; break; case 2: $hve['d'] = 'vn'; break; } return $hve; } /*/////////////////////////////////////////////////////////////////////////////////////////////////*/ /*| initialize variables |*/ /*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*/ $ac['mp'] = $ac['m'] % 2 !== 0 ? 0: 1; $ac['np'] = $ac['n'] % 2 !== 0 ? 0: 1; $ac['m+1'] = $ac['m']+1; $ac['n+1'] = $ac['n']+1; $ac['m-1'] = $ac['m']-1; $ac['n-1'] = $ac['n']-1; $cv = calculate_center($ac['m'], $ac['n'], $ac['v'], $ac['mp'], $ac['np']); $ac['cv'] = count($cv['t']); /* central vertices */ /* edges = (m-1)n + (n-1)m = 2mn - m - n */ $ac['e']['x'] = ($ac['n']-1) * $ac['m']; $ac['e']['y'] = ($ac['m']-1) * $ac['n']; $ac['e']['s'] = $ac['e']['x'] + $ac['e']['y']; $ac['e']['z'] = ($ac['m']-1) * ($ac['n']-1); if ($ac['q']) { $ac['e']['z'] = pow($ac['n']-1, 2); } $rv = array (); /* random vertex edge selections */ /* s = start, g = goal, m = row, n = column, i = icon, d = difference */ $nodes['s']['m'] = randomize(1, $ac['m'], 'random_int'); $nodes['s']['n'] = randomize(1, $ac['n'], 'random_int'); $nodes['s']['mn'] = $nodes['s']['m'].','.$nodes['s']['n']; $nodes['s']['i'] = '🙂'; $nodes['g']['i'] = '🔒'; $nodes['g']['m'] = randomize(1, $ac['m'], 'random_int'); $nodes['g']['n'] = randomize(1, $ac['n'], 'random_int'); $nodes['g']['mn'] = $nodes['g']['m'].','.$nodes['g']['n']; if ($nodes['g']['m'] === $nodes['s']['m']) { /* removing the if branch forces separate columns regardless of m values */ while ($nodes['g']['n'] === $nodes['s']['n']) { $nodes['g']['n'] = randomize(1, $ac['n'], 'random_int'); } } $nodes['m']['d'] = abs($nodes['s']['m'] - $nodes['g']['m']); $nodes['n']['d'] = abs($nodes['s']['n'] - $nodes['g']['n']); /* perimeters array defining borders for mn neighbor calculations and other routines */ $p['N'] = perimeters($ac['m'], $ac['n'], $ac['v'], 'N', 0); $ac['p']['N'] = (int) count($p['N']); $p['W'] = perimeters($ac['m'], $ac['n'], $ac['v'], 'W', 0); $ac['p']['W'] = (int) count($p['W']); $p['S'] = perimeters($ac['m'], $ac['n'], $ac['v'], 'S', 0); $ac['p']['S'] = (int) count($p['S']); $p['E'] = perimeters($ac['m'], $ac['n'], $ac['v'], 'E', 0); $ac['p']['E'] = (int) count($p['E']); /* unique perimeters containing all cardinal points */ $p['u'] = perimeters($ac['m'], $ac['n'], $ac['v'], '', 1); $ac['p']['u'] = (int) count($p['u']); /* corner (c) vertices of perimeters */ $p['c'] = (array) [ 'NW' => $p['N'][array_key_first($p['N'])], 'NE' => $p['N'][array_key_last($p['N'])], 'SW' => $p['S'][array_key_first($p['S'])], 'SE' => $p['S'][array_key_last($p['S'])] ]; /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ /* define the grid graph, vertex cost, vertex neighbors, cycles and edges */ /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ for ($i = 1; $i <= $ac['m']; $i++) { for ($j = 1; $j <= $ac['n']; $j++) { $t++; $nwse = 0; $ggt[$t]['xy']['m'] = $i; $ggt[$t]['xy']['n'] = $j; $ggt[$t]['xy']['a'] = $i.$j; $ggt[$t]['xy']['mn'] = $i.','.$j; /* pre-calculate f(n) = g(n) + h(n) */ $ggt[$t]['a*']['g'] = manhattan_distance($i, $nodes['s']['m'], $j, $nodes['s']['n']); $ggt[$t]['a*']['h'] = manhattan_distance($i, $nodes['g']['m'], $j, $nodes['g']['n']); $ggt[$t]['a*']['f'] = $ggt[$t]['a*']['g'] + $ggt[$t]['a*']['h']; /* calculate neighbors utilizing perimiter checks */ if (empty($p['N'][$t])) { $ggcd[$t]['N'] = $t - $ac['n']; $nwse++; } if (empty($p['W'][$t]) && empty($p['N'][$t])) { $ggod[$t]['NW'] = $t - $ac['n+1']; } if (empty($p['E'][$t]) && empty($p['N'][$t])) { $ggod[$t]['NE'] = $t - $ac['n-1']; } if (empty($p['W'][$t])) { $ggcd[$t]['W'] = $t - 1; $nwse++; } if (empty($p['S'][$t])) { $ggcd[$t]['S'] = $t + $ac['n']; $nwse++; } if (empty($p['W'][$t]) && empty($p['S'][$t])) { $ggod[$t]['SW'] = $t + $ac['n-1']; } if (empty($p['E'][$t]) && empty($p['S'][$t])) { $ggod[$t]['SE'] = $t + $ac['n+1']; } if (empty($p['E'][$t])) { $ggcd[$t]['E'] = $t + 1; $nwse++; } if ($nwse === 4) { $cyan['cr'][$t] = $t; } /* crossroads or 4 degree vertices */ /* South East Method: E border = v edges only and S border = h edges only. */ if ($j < $ac['n']) { $gge['hm'][$t] = 0; } if ($i < $ac['m']) { $gge['vn'][$t] = 0; } if ($j < $ac['n'] && $i < $ac['m']) { if ($i < $ac['m-1']) { $cyan['cy'][$i][$j] = (array) [$t => 0, $t+1 => 0]; continue; } $cyan['cy'][$i][$j] = (array) [$t => 0, $t+1 => 0, $t+$ac['n'] => 0]; } /* mn cycles */ }} if (!empty($cyan['cr'])) { $ac['cr'] = count($cyan['cr']); } /* count crossroads. 2x2 is empty */ $ac['e']['hm'] = count($gge['hm']); $ac['e']['vn'] = count($gge['vn']); $nodes['s']['t'] = $gg[$nodes['s']['m']][$nodes['s']['n']]['t']; $nodes['g']['t'] = $gg[$nodes['g']['m']][$nodes['g']['n']]['t']; if ($algor['cb']) { /* cycle breaker */ for ($i = 1; $i <= $ac['m-1']; $i++) { for ($j = 1; $j <= $ac['n-1']; $j++) { $rv = cbr($cyan['cy'][$i][$j], $gge, 'randomize'); $gge[$rv['d']][$rv['t']] = 1; $gge['c']++; }} } /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ /* large plate of svg spaghetti with meatballs code */ /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ echo '<svg width="'.($ac['n']*90).'" height="'.($ac['m']*90+10).'" xmlns="http://www.w3.org/2000/svg">'.PHP_EOL; // style="background-color: rgb(0,0,0,0.1);" echo '<style>.edge { fill: #96ade9; fill-opacity: 0.5; stroke-width: 2; stroke: #96ade9; } .bg101010 { fill: #f0f0f0; fill-opacity: 0.8; stroke-width: 2px; stroke: #96ade9; } .bg808080 { fill: #808080; fill-opacity: 0.8; } .bg00a000 { fill: #00a000; fill-opacity: 0.8; } </style>'.PHP_EOL; for ($i = 1; $i <= $ac['m']; $i++) { $svg['rx'] = 0; $svg['e']['hx'] = 80; $svg['e']['vx'] = 30; for ($j = 1; $j <= $ac['n']; $j++) { if ($gg[$i][$j]['t'] === $nodes['s']['t']) { $svg['sx'] = $svg['rx']+40; $svg['sy'] = $svg['ry']+40; $svg['mx'] = ($svg['sx']-8); } if (!empty($path[$gg[$i][$j]['t']])) { $svg['rf'] = 'bg00a000'; } echo "<rect width='80' height='80' x='".$svg['rx']."' y='".$svg['ry']."' rx='10' ry='10' class='".$svg['rf']."'><title>Tile " . $gg[$i][$j]['t'] ." (" . $i . ", " . $j . ")</title></rect>".PHP_EOL; if ($i.$j === $nodes['g']['m'].$nodes['g']['n']) { echo "<text x='".($svg['rx'] + 40)."' y='".($svg['ry'] + 40)."' textLength='100%' font-size='28' dominant-baseline='middle' text-anchor='middle'>".$nodes['g']['i']."</text>".PHP_EOL; } if ($j < $ac['n'] && empty($gge['hm'][$gg[$i][$j]['t']])) { echo "<rect width='10' height='20' x='".$svg['e']['hx']."' y='".$svg['e']['vy']."' rx='0' ry='0' class='edge' />".PHP_EOL; } if ($i < $ac['m'] && empty($gge['vn'][$gg[$i][$j]['t']])) { echo "<rect width='20' height='10' x='".$svg['e']['vx']."' y='".$svg['e']['hy']."' rx='0' ry='0' class='edge' />".PHP_EOL; } $svg['rx']+= 90; $svg['e']['hx']+= 90; $svg['e']['vx']+= 90; $svg['rf'] = 'bg101010'; } $svg['ry']+= 90; $svg['e']['hy']+= 90; $svg['e']['vy']+= 90; } echo '<defs>'.PHP_EOL; echo ' <g id="start_node">'.PHP_EOL; echo ' <style>'.PHP_EOL; echo ' .smiley_face { fill: #ffcc33; stroke: #666633; stroke-width: 2; }'.PHP_EOL; echo ' .smiley_eyes { fill: #000000; stroke: #333333; stroke-width: 1; }'.PHP_EOL; echo ' .smiley_eyes_white { fill: #f0f0f0; }'.PHP_EOL; echo ' </style>'.PHP_EOL; echo ' <title>Tile ' . $nodes['s']['t'] . ' (' . $nodes['s']['m'] . ', ' . $nodes['s']['n'] . ')</title>'.PHP_EOL; echo ' <circle r="16" cx="'.$svg['sx'].'" cy="'.$svg['sy'].'" class="smiley_face" />'.PHP_EOL; echo ' <circle r="3" cx="'. ($svg['sx'] -5) .'" cy="'. ($svg['sy'] -3) .'" class="smiley_eyes" />'.PHP_EOL; echo ' <circle r="1" cx="'. ($svg['sx'] -4) .'" cy="'. ($svg['sy'] -4) .'" class="smiley_eyes_white" />'.PHP_EOL; echo ' <circle r="3" cx="'. ($svg['sx'] + 6) .'" cy="'. ($svg['sy'] -3) .'" class="smiley_eyes" />'.PHP_EOL; echo ' <circle r="1" cx="'. ($svg['sx'] + 7) .'" cy="'. ($svg['sy'] -4) .'" class="smiley_eyes_white" />'.PHP_EOL; echo ' <path d="M'.$svg['mx'].','.($svg['sy']+7).' Q'.($svg['mx']+8).','.($svg['sy']+14).' '.($svg['mx']+16).','.($svg['sy']+7).'" fill="none" stroke="#000000" stroke-width="2" />'.PHP_EOL; echo ' </g>'.PHP_EOL; echo '</defs>'.PHP_EOL; echo '<use href="#start_node" />'.PHP_EOL; echo '</svg>'.PHP_EOL; echo '<br>'.PHP_EOL; echo '<p>start tile ' . $nodes['s']['t'] . ' = m' . $nodes['s']['m'] . ', n' . $nodes['s']['n'] . ' (g = 0)<br>'; echo 'goal tile ' . $nodes['g']['t'] . ' = m' . $nodes['g']['m'] . ', n' . $nodes['g']['n'] . ' (h = 0)</p>'; echo 'edges: ' . $ac['e']['s'] . ' (' . $ac['e']['x'] . ' hm, ' . $ac['e']['y'] . ' vn)<br>'; echo 'cycles: ' . $ac['e']['z'] . ' (' . $ac['m-1'] . ' m sets * ' . $ac['n-1'] . ' n sets)<br>'; echo 'cycle disconnections: ' . $gge['c'] . '<br>'; /* debugging */ if ($ggDisplay) { echo '<p>grid graph main array:<br>'; print_r($ggt); echo '</p>'; echo '<p>f(n) g(n) h(n):<br>'; for ($i = 1; $i <= $ac['v']; $i++) { echo 'tile ' . $i . ' (' . $ggt[$i]['xy']['mn'] . '): f = ' . $ggt[$i]['a*']['f'] . ', g = ' . $ggt[$i]['a*']['g'] . ', h = ' . $ggt[$i]['a*']['h'] . '<br>'; } echo '</p>'; echo '<p>vertex adjacency of cardinal directions:<br>'; print_r($ggcd); echo '</p>'; echo '<p>vertex adjacency of ordinal directions:<br>'; print_r($ggod); echo '</p>'; echo '<p>accounting:<br>'; print_r($ac); echo '</p>'; echo '<p>perimeters:<br>'; print_r($p['u']); echo '</p>'; echo '<p>Northern perimeters:<br>'; print_r($p['N']); echo '</p>'; echo '<p>Western perimeters:<br>'; print_r($p['W']); echo '</p>'; echo '<p>Southern perimeters:<br>'; print_r($p['S']); echo '</p>'; echo '<p>Eastern perimeters:<br>'; print_r($p['E']); echo '</p>'; echo '<p>corners:<br>'; print_r($p['c']); echo '</p>'; echo '<p>central vertices:<br>'; print_r($cv); echo '</p>'; echo '<p>cycles:<br>'; print_r($cyan['cy']); echo '</p>'; echo '<p>crossroads:<br>'; print_r($cyan['cr']); echo '</p>'; echo '<p>edges hm:<br>'; print_r($gge['hm']); echo '</p>'; echo '<p>edges vn:<br>'; print_r($gge['vn']); echo '</p>'; } /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ /* The End. */ /* ////////////////////////////////////////////////////////////////////////////////////////////////*/ ?> for Barand: function cbr and if algor cb is my non path finding solution. n-1^2 sets the stage.
-
where is your database code? it would be nice to see your query in addition to expected results.