Jump to content

jodunno

Members
  • Posts

    293
  • Joined

  • Last visited

  • Days Won

    5

Community Answers

  1. jodunno's post in count down a specific period to zero time was marked as the answer   
    so you have some code to write...
  2. jodunno's post in how to print once,get out of loop and continue with rest inside same loop was marked as the answer   
    if data is static and you are positive that this is the code that you wish to implement, then your logic needs to change from less than two to a more precise identifier. A switch will work better for you and allow you to adjust the code if you add more items to the array (which should not contain curly braces, rather square brackets).
    <?php $data=["egg","york","pork"]; $total = count($data); for ($i = 0; $i < $total; $i++) { switch ($i) { case 0: echo '<p>SECTION A<br>'; break; case 2: echo '</p>SECTION B<br>'; break; } echo $data[$i] . '<br>'; } $data2 = (array) ['Section A' => (array) ["egg","york"], 'Section B' => (array) ["pork"]]; foreach ($data2 as $section => $array) { echo '<p>' . $section . '<br>'; foreach ($array as $type) { echo $type.'<br>'; } echo '</p>'; } ?> but i recommend that you build your arrays to structure and identify data, then looping will be easy and the resulting code will be cleaner and more logical. vide data2 array and the subsequent code above.
  3. jodunno's post in how to merge two arrays in order was marked as the answer   
    again with these arrays? you'll never learn, eh?
    I think that array merge will fail you here with these demands and array combine will throw an error due to the key length mismatches. I can only guess that you are trying to preserve the order of array 11, id est, the key 1 not following 0. I also assume that you want the 10 key moved to array 2 1 key. This is really not how one should code arrays. I can only suggest bad coding practices in an attempt to solve your problem but i may not be understanding you correctly. Very vague.
    I have a way to mix the arrays but it is not professional coding methods by any stretch of the imagination. Yes i'm saying that the following code is bad. Meantime, the final key can be printed to the screen but outside of the loop. Inside of the loop would require an array count and a counter to find the end of the loop.
    <?php echo <<<RAYS <html> <head> <title></title> <style> div.raybox { display: block; position: static; width: 20em; height: 18em; vertical-align: top; background: #101010; border-radius: 8px; box-sizing: content-box; overflow: hidden; } div.ray { display: inline-block; position: relative; margin: 4px; padding: 4px; width: 9em; height: 94%; vertical-align: top; background: #000000; color: rgba(240, 240, 240, 0.6); box-sizing: content-box; } </style> </head> <body> RAYS; $aRay = (array) [0 => (array) [0 => "a", 1 => "b", 2 => "c", 3 => "d", 4 => "e", 5 => "f", 6 => "g", 7 => "h", 8 => "i", 9=> "j"], 1 => (array) [0 => "a", 1 => ""]]; $bRay = (array) [0 => "1.1.51", 2 => "1.1.51.2", 3 => "1.1.51.3", 4 => "1.1.51.4", 5 => "1.1.51.5", 6 => "1.1.51.6", 7 => "1.1.51.7", 8 => "1.1.51.8", 9 => "1.1.51.9", 1 => "1.1.51.10", 10 => "1.1.52"]; echo '<div class="raybox">'; echo '<div class="ray">aRay:<br>'; foreach ($aRay as $key => $array) { foreach ($array as $index => $value) { echo '[' . $key . '][' . $index . '] => ' . $value . '<br>'; } } echo '</div>'; echo '<div class="ray">bRay:<br>'; foreach ($bRay as $index => $value) { echo '[' . $index . '] => ' . $value . '<br>'; } echo '</div>'; echo '</div>'; $aRay[1][1] = array_pop($bRay); $stingRay = array (); array_push($stingRay, $bRay); array_push($stingRay, $aRay[0]); array_push($stingRay, $aRay[1]); //echo '<br>'; //print_r($stingRay); //echo '<br>'; //foreach ($stingRay as $key1 => $array) { // foreach ($array as $key2 => $value) { // echo $value . '<br>'; // } //} echo '<br>'; foreach ($stingRay[0] as $key => $value) { echo $value . ': '; echo $stingRay[1][$key] . '<br>'; } echo $stingRay[2][1] . ': ' . $stingRay[2][0]; echo <<<RAYS </body> </html> RAYS; ?> the output is what you are seeking, i think. Otherwise, i would say build a new array with array 11 values as keys in the new array. Then add array 2 values (letters) as values for the new array keys. Then add the last key value pair to the new array. Then you could loop over one array printing keys and values to obtain your objective. 
    I'm ashamed to post this awful code but that is how twisted this concept is becoming. Really try to recode your project correctly and these problems disappear. 
  4. jodunno's post in how to loop through array was marked as the answer   
    I would not design a site in such a manner. I would recognize the errors in my ways and fix them. 'been there, done that' is a common expression that applies here.
    But because you asked:
    <?php $p_id='1.1.51|||1.1.51.2|||1.1.51.3|||1.1.51.4|||1.1.51.5|||1.1.51.6|||1.1.51.7|||1.1.51.8|||1.1.51.9|||1.1.52|||1.1.52.2'; $Q_Id = explode('|||', $p_id); $m = count($Q_Id); $theme = 'b|||c|||d|||e|||f|||g|||h|||i|||j|||k'; $sub = explode(',', $theme); $sub = explode('|||', $sub[0]); $sub2[] = 'a'; $sub = array_merge($sub2, $sub); $n = count($sub) - 1; //print_r($Q_Id); echo '<br>'; //print_r($sub); echo '<br>'; $j = 0; $k = substr($Q_Id[0], 0, 6); for ($i = 0; $i < $m; $i++) { $index = substr($Q_Id[$i], 0, 6); if ($index !== $k) { $j = 0; $k = substr($Q_Id[$i], 0, 6); echo '----- looky here ' . $k . '<br>'; } echo $Q_Id[$i] . ': ' . $sub[$j] . '<br>'; $j++; } ?> I do not understand why you have these ids in a variable. You should store them in an array by an index system so that you know when to start back at the letter a. However, the English alphabet runs out of letters at 26 so do you plan on having a solution for that too? Do you see how such code leads us down a dark spiraling vertex of madness? a black hole. Even light cannot escape this madness. Change your code as soon as possible.
    Meantime, good luck with all of that and goodnight. I am in a different timezone and it's almost bedtime.
    edit: to avoid more questions, $k has been set to the value of array key 0.
  5. jodunno's post in Can't get login and file uploads to work was marked as the answer   
    so i had some free time to code a better example page for you while preserving some of your original content. I strongly disagree that you need to submit the form to the same page [rolling my eyes] but do whatever feels best for you.
    <?php declare (strict_types = 1);   // if (empty($_SESSION['userID']) { header("Location: /"); exit; }   (string) $SID_page = 'getForm'; (string) $SID_pageTitle = 'Upload files';   switch ($_SERVER["REQUEST_METHOD"]) {     case 'POST':          (string) $SID_errors = '';          (array) $SID_filesArrayErrors = ['4'=>'Please choose a file for upload'];           if (!empty($_FILES['Upload']['error'])) { if (array_key_exists($_FILES['Upload']['error'], $SID_filesArrayErrors)) { $SID_errors = $SID_filesArrayErrors[$_FILES['Upload']['error']]; break; } $SID_errors = 'fileArray'; break; }          if (preg_match('/^[0-9A-Za-z-_.\s]{1,64}.[jpg|jpeg|jpe|png|gif]+$/', $_FILES['Upload']['name']) === 0) { $SID_errors = 'Invalid filename (Filenames must be Alphanumeric with the following acceptions: - _ . and word spaces)'; break; }          if (function_exists(pathinfo($_FILES['Upload']['name'], PATHINFO_FILENAME))) { /* log this error */ $SID_errors = 'hack attempt'; break; }          /* catches built-in functions: phpinfo, phpinfo(), file_get_contents et cetera. use prefixes for all user defined code (here $SID_, stands for site id) */          $SID_dir = 'folder/';          $SID_timestamp = time();          $SID_filename = $SID_dir . $SID_timestamp.basename($_FILES['Upload']['name']);          move_uploaded_file($_FILES['Upload']['tmp_name'], $SID_filename);          $SID_page = 'postForm'; $SID_pageTitle = 'Files upload';     break;   } ?> <html> <head>   <title><?php if (!empty($SID_pageTitle)) { echo $SID_pageTitle; } ?></title> </head> <body> <?php switch ($SID_page) { case 'getForm': ?> <div data-role="page" id="page">   <div data-role="header">    <h1>Upload files</h1>    <a href="logout.php" data-role="button" data-icon="home">Sign out</a>    </div>   <div data-role="content"> <?php if (!empty($SID_errors)) { echo '<div><p>' . $SID_errors . '</p></div>'; } ?>   <form method="post" enctype="multipart/form-data">         <input type="file" name="Upload">         <input type="submit">     </form>   </div>   </div> <?php break; case 'postForm':   /* var_dump($_FILES); */   echo '<p>File was uploaded --> '. htmlspecialchars(urlencode($_FILES['Upload']['name']), ENT_QUOTES);   echo '<br>';   echo '<p>Information about file from $FILE array</p>';   echo 'File Name: ' . htmlspecialchars(urlencode($_FILES['Upload']['name']), ENT_QUOTES) . '<br>';   echo 'File Type: ' . htmlspecialchars($_FILES['Upload']['type'], ENT_QUOTES) . '<br>';   echo 'File Size: ' . htmlspecialchars($_FILES['Upload']['size'], ENT_QUOTES) . 'kB<br>';   /* one could add the form here too for more uploads but a user limit should be placed in a database      then loaded into the session at login or retrieved from the db on pageload */  break; } ?> </body> </html> good luck to you. You may want to ask requinix about my regex code. I am not a programmer either, so perhaps my regex could be better. Have a nice day.
×
×
  • 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.