Jump to content

Barand

Moderators
  • Posts

    24,551
  • Joined

  • Last visited

  • Days Won

    821

Community Answers

  1. Barand's post in Form Validation was marked as the answer   
    Your select element does not have a name.
    Your checkbox doesn't have a value. ( I suggest "1")
    Only checked checkboxes are posted.  You can use
    $checkbox = $_POST['checkbox'] ?? 0; Then $checkbox will contain 1 if checked and 0 if not.
  2. Barand's post in how receive specific price from json to php was marked as the answer   
    See
     
  3. Barand's post in Trying to convert column from full name field to first and last name fields was marked as the answer   
    explode requires two parameters
    explode(delimiter, array)
    RTFM
  4. Barand's post in Learning PHP, Java Dev with a headache. was marked as the answer   
    You will actually need both the id and the name
    The label/for attributes use the id, POST variables use the name.
  5. Barand's post in Not inserting data in table was marked as the answer   
    You can do with a single INSERT SELECT statement.
    INSERT INTO Images (name, person_id, filename, description, medium) SELECT name , table_id , image_name , description , medium FROM tbl_person_data; Doing it with a prepared statement, as you are, requires either
    execute with a numerically indexed array and ? as placeholders, or execute with an associative array and named placeholders matching the array keys
  6. Barand's post in Group Timestamps in array into unique instances and count them via php was marked as the answer   
    try
    foreach ($array as $k => $d) { if ($k > 0) { if (strtotime($d) > strtotime($array[$k-1])+6) { $new[] = "-------------------"; } } $new[] = $d; }  
    $new = Array ( [0] => 2021-02-10 09:04:48 [1] => 2021-02-10 09:04:54 [2] => 2021-02-10 09:05:00 [3] => 2021-02-10 09:05:06 [4] => 2021-02-10 09:05:12 [5] => 2021-02-10 09:05:18 [6] => ------------------- [7] => 2021-02-10 09:06:18 [8] => 2021-02-10 09:06:24 ) [edit...]
    Alternative solution...
    $new = []; $newkey = 0; foreach ($array as $k => $d) { if ($k > 0) { if (strtotime($d) > strtotime($array[$k-1])+6) { $newkey++; } } $new[$newkey][] = $d; } gives
    $new = Array ( [0] => Array ( [0] => 2021-02-10 09:04:48 [1] => 2021-02-10 09:04:54 [2] => 2021-02-10 09:05:00 [3] => 2021-02-10 09:05:06 [4] => 2021-02-10 09:05:12 [5] => 2021-02-10 09:05:18 ) [1] => Array ( [0] => 2021-02-10 09:06:18 [1] => 2021-02-10 09:06:24 ) )  
  7. Barand's post in Need help - php download from remote URL was marked as the answer   
    UK Script - downloads from local UK file server and DB connection is to (remote) US DB server US Script - downloads from local US file server and DB connection is to (local) US DB server.
  8. Barand's post in how to echo out a value from json array was marked as the answer   
    If you
    echo '<pre>$json = ', print_r($json, 1), '</pre>'; you get
    $json = Array ( [terms] => http://www.xe.com/legal/dfs.php [privacy] => http://www.xe.com/privacy.php [from] => USD [amount] => 1.195 [timestamp] => 2021-02-09T16:52:00Z [to] => Array ( [0] => Array ( [quotecurrency] => NGN [mid] => 454.6559871014 ) ) ) Then follow the array keys to the value you want

    So you need
    echo $json['to'][0]['mid']; // --> 454.6559871014 [edit] Alternatively
    foreach ($json['to'] as $price) { printf('%s %0.2f<br>', $price['quotecurrency'], $price['mid'] ); // --> NGN 454.66 }  
  9. Barand's post in sql query, select age range and show none information was marked as the answer   
    Try doing it as I did.
  10. Barand's post in array_push and array_chunk difficulties was marked as the answer   
    Are you wanting something like this?
    function golfGroups($players) { $n = count($players); if ($n < 6) return $players; $num_3s = $n%4 ? 4 - $n%4 : 0; $num_remain = $n - $num_3s*3; $fours = array_chunk(array_slice($players, 0, $num_remain), 4); $threes = $num_3s ? array_chunk(array_slice($players, -$num_3s*3), 3) : []; return array_merge($fours, $threes); } ## ## TEST IT ## for ($i=6; $i<=25; $i++) { $p = range(1,$i); $p = array_map( function($v) { return "Player $v";}, $p); // create array of players echo "<h3>$i players</h3>"; echo '<pre>', print_r(golfGroups($p), 1), '</pre>'; echo '<hr>'; }  
  11. Barand's post in Cannot Update MySQLI unless I upload Image was marked as the answer   
    Both updates happen only when
    if(!empty($_FILES['image']['name'])) { ...
  12. Barand's post in This is a pain in the bottom, everything should do as is. was marked as the answer   
    Probably because the inner loop's result object is overwriting the outer result object as you store them both in $query.
    But don't run queries inside loops like that . Use a single query with a join to get all the data in a single query. It's far more efficient.
    SELECT faqc_name , faq_id , faq_question FROM faq_cats c JOIN faqs f ON f.faq_cat = c.faqc_id WHERE faqc_site = :site ORDER BY faqc_id , faq_id  
  13. Barand's post in Broadcast Calendar Week Number was marked as the answer   
    Here's my attempt
    function TVWeek($d = null) { $dt = new DateTime($d); $y = $dt->format('Y'); $mon1 = new DateTime("$y-01-01"); // last week of year condition ++$y; $nextd1 = new DateTime("$y-01-01"); if ($dt->format('W') == $nextd1->format('W')) return 1; if ($mon1->format('w') != 1) { $mon1->modify('last monday'); } return intdiv( $mon1->diff($dt)->days, 7 ) + 1; } echo '<br>' . TVWeek(); // 1 echo '<br>' . date('W'); // 53 echo '<br>' . TVWeek('2020-06-01'); // 23 echo '<br>' . date('W', strtotime('2020-06-01')); // 23 echo '<br>' . TVWeek('2016-06-01'); // 23 echo '<br>' . date('W', strtotime('2016-06-01')); // 22  
  14. Barand's post in MySQL Query Works in PHPMyAdmin But Doesn't Work in a PHP File was marked as the answer   
    Try changing the
    `tran_term_taxonomy`.`description` = "" to
    `tran_term_taxonomy`.`description` = '' It won't like you using " in a string enclosed by ".."
  15. Barand's post in each() function is deprecated... how can I convert the each in this code please... stuck! was marked as the answer   
    Best way to traverse an array is with foreach().
    $fileArr = []; $files = glob("$masterdir/*.jpg"); foreach ($files as $fname) { $t = filemtime($fname); $fileArr{$fname} = $t; } arsort($fileArr); foreach (array_slice($fileArr, 0, 7) as $thisName => $thisTime) { // iterate through first 7 files echo "$thisName &mdash; " . date("d M y", $thisTime) . '<br>'; }  
  16. Barand's post in Encountering this error... was marked as the answer   
    Strange, ENT_IGNORE was added in v5.3. However its use is discouraged as a potential security risk.
    Try replacing ENT_IGNORE with NULL
    EDIT: As of 5.4, UTF-8 is the default so just go with htmlspecialchars($tagContent)
  17. Barand's post in Count duplicate adjacent (Array) was marked as the answer   
    try
    $arr = array("test", "test", "hello", "test", "world", "world", "world", "hello", "test"); $prev = ''; $results = []; $j = 0; foreach ($arr as $i => $v) { if ($v != $prev) { $j = $i; $results[$j] = 1; $prev = $v; } else { $results[$j]++; } } foreach ($results as $k => $v) { echo "$arr[$k] : $v <br>"; } Result:
    test : 2 hello : 1 test : 1 world : 3 hello : 1 test : 1  
  18. Barand's post in Fatal error: Uncaught Error: Call to a member function close() on bool Stack trace: #0 {main} was marked as the answer   
    I'm going to say your $stmt->prepare failed, so $stmt does not contain a valid statement object, hence the $stmt->close() failure.
    Reason for it failing - maybe because you create a table called "todolist" then try to insert into a table called "employees"
  19. Barand's post in Single and double quotes for echo lists of a tags retrieving data from dbase was marked as the answer   
    Use {..} around the complex variables, e.g.
    echo "<li><a href='{$row['url']}' title='{$row['title']}'><i class='fas fa-user site-nav--icon'></i> Help</a></li>"; ^___________^ ^_____________^  
  20. Barand's post in Calling functions in other files was marked as the answer   
    Perhaps it's something to do with...

  21. Barand's post in Loop an array help was marked as the answer   
    Use array_chunk() to break up the array into many arrays of 100 in each
    EG
    $data = range(1, 250); $chunks = array_chunk($data, 25); foreach ($chunks as $chunk) { foreach ($chunk as $n) { printf('%03d ', $n); } echo '<br>'; } Giving
    001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 021 022 023 024 025 026 027 028 029 030 031 032 033 034 035 036 037 038 039 040 041 042 043 044 045 046 047 048 049 050 051 052 053 054 055 056 057 058 059 060 061 062 063 064 065 066 067 068 069 070 071 072 073 074 075 076 077 078 079 080 081 082 083 084 085 086 087 088 089 090 091 092 093 094 095 096 097 098 099 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 The 318000 items aren't coming from another table, are they?
  22. Barand's post in How to get the ip address from an Html form? was marked as the answer   
    Most servers are configured so that files with names ending with ".php" are processed by the PHP preprocessor. Unless your server is specifically configured to do so, ".html" files will not be processed and any php code is treated a text.
  23. Barand's post in PHP Multidimensional Arrays was marked as the answer   
    This will set an active flag in your elements array
    $res = $db->query("SELECT article_id , branch_id , article_name , 0 as active FROM article"); $Elements = []; foreach ($res as $el) { $Elements[$el['article_id']] = $el; } $activeId = 50; setActiveElement($Elements, $activeId); function setActiveElement(&$els, $id) { $els[$id]['active'] = 1; if ($els[$id]['branch_id'] != 0) { setActiveElement($els, $els[$id]['branch_id']); } } As you can see, 1, 38 and 50 are now set "active"
     
    Array ( [1] => Array ( [article_id] => 1 [branch_id] => 0 [article_name] => Knowledge Base - How To [active] => 1                                                          <--------------- ) [38] => Array ( [article_id] => 38 [branch_id] => 1 [article_name] => Bugs [active] => 1                                                          <--------------- ) [46] => Array ( [article_id] => 46 [branch_id] => 0 [article_name] => Client Information [active] => 0 ) [49] => Array ( [article_id] => 49 [branch_id] => 1 [article_name] => Features [active] => 0 ) [50] => Array ( [article_id] => 50 [branch_id] => 38 [article_name] => Another Branch [active] => 1                                                          <--------------- ) [52] => Array ( [article_id] => 52 [branch_id] => 0 [article_name] => @Test [active] => 0 ) [53] => Array ( [article_id] => 53 [branch_id] => 52 [article_name] => testing [active] => 0 ) [54] => Array ( [article_id] => 54 [branch_id] => 46 [article_name] => Testing Audit [active] => 0 ) [55] => Array ( [article_id] => 55 [branch_id] => 0 [article_name] => Testing [active] => 0 ) [57] => Array ( [article_id] => 57 [branch_id] => 55 [article_name] => New Article [active] => 0 ) )
  24. Barand's post in Valdiation was marked as the answer   
    you could try using the "required" property
    <select name="select" required> <option value="">Please select the level</option> <option value="Administrator">Administrator</option> <option value="License">License</option> <option value="Scorer">Scorer</option> <select>
  25. Barand's post in Remove related element from multidimensional array was marked as the answer   
    Rearrange the array so you gather the values for each record together
    $other_image = Array ( 'img' => Array ( '0' => '1526973657.jpg', '1' => '1526973661.jpg', '2' => '1526973665.jpg' ), 'path' => Array ( '0' => '../post-upload/1/', '1' => '../post-upload/1/', '2' => '../post-upload/1/' ), 'type' => Array ( '0' => 1, '1' => 1, '2' => 1 ), 'thumb' => Array ( '0' => 'thumb_1526973661.jpg', '1' => 'thumb_1526973665.jpg', '2' => 'thumb_1526973668.jpg' ) ); $record_vals = []; // values for each record to be inserted foreach ($other_image as $name => $values) { foreach ($values as $k => $v) { $record_vals[$k][$name] = $v; } } Now you have an array that you can loop through to insert the records
    $record_vals = Array ( [0] => Array ( [img] => 1526973657.jpg [path] => ../post-upload/1/ [type] => 1 [thumb] => thumb_1526973661.jpg ) [1] => Array ( [img] => 1526973661.jpg [path] => ../post-upload/1/ [type] => 1 [thumb] => thumb_1526973665.jpg ) [2] => Array ( [img] => 1526973665.jpg [path] => ../post-upload/1/ [type] => 1 [thumb] => thumb_1526973668.jpg ) )
×
×
  • 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.