janka Posted July 14, 2020 Report Posted July 14, 2020 Labdien, Izmantoju šādu skriptu, kas no saišu saraksta ģenerē dažas, kuras attēlot. <?php function dodosrandgen() { // set number of links you want to show at once $show_link = 2; // separate the links by, i.e. comma would be ","; $separator = " "; define('link', 'link'); define('priority', 'priority'); $links[0]['link'] = "<li><a href=\"http://www.delfi.lv/\">Delfi</a></li>"; $links[0]['priority'] = 5; $links[1]['link'] = "<li><a href=\"http://tvnet.lv/\">Tvnet</a></li>"; $links[1]['priority'] = 1; $links[2]['link'] = "<li><a href=\"http://php.lv\">Php</a></li>"; $links[2]['priority'] = 2; $links[3]['link'] = "<li><a href=\"http://apollo.lv/\">Apollo</a></li>"; $links[3]['priority'] = 3; $links[4]['link'] = "<li><a href=\"google.com/\">Google</a></li>"; $links[4]['priority'] = 4; // HERE COMES THE BODY if($links) { for($i = -1; $i < count($links); $i++) { for($j = 0; $j < $links[$i]['priority']; $j++) { $newlinksindex = count($newlinks) + 1; $newlinks[$newlinksindex] = $links[$i]['link']; } } } // end of if link exist // debug if($show_link >= count($newlinks)) $show_link = count($newlinks); $showarray[0] = ""; $showindex = 0; // Select a random element from the new array do { $randindex = rand(0, count($newlinks)); if(!in_array($newlinks[$randindex], $showarray) && $newlinks[$randindex] != "") { $showarray[$showindex] = $newlinks[$randindex]; //print_r($showarray); $showindex++; } }while(count($showarray) <= $show_link); // finally print out for($i = 0; $i < $show_link; $i++ ) { if($show_link == 1 || (($i + 1) == $show_link)) print $showarray[$i]; else print $showarray[$i].$separator; } //print_r($ranarray); //print_r($links); //print_r($newlinks); } // end of the dodosrandgen function ?> <?php dodosrandgen();?> Skriptam ir šādas problēmas: Quote Notice: Undefined offset: -1 in /home2/domain/public_html/a.php on line 28 Notice: Undefined variable: newlinks in /home2/domain/public_html/a.php.php on line 29 Paša mēģinājumi tās novērst noved pie vēl sliktāka iznākuma. Priecāšos par palīdzību. Paldies. Quote
e-remit Posted July 14, 2020 Report Posted July 14, 2020 Kāpēc tev 27. rindā $i sāk skaitīt ar -1? Tak noticē teikts, ka 28. rindā mēģini masīvam dabūt elementu ar indeksu -1. Un otrajā noticē teikts, ka mēģini izmantot mainīgo, kurš nekur iepriekš nav definēts. Iesaku palasīt PHP manuāli par masīviem! Uzzināsi daudz jauna! Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.