sunshine Posted September 13, 2007 Share Posted September 13, 2007 Hello. I'm trying to generate a random list within my HTML. I've done random lists before, but this is giving me problems. In the code below, $file should be printing the array I have stored in $file. $file contains raw HTML, comprised of about 10 items right now, but will be higher as I add to it. (That is why I did the $arraycount). I did a var_dump($file) and I can see that $file does indeed contain what I want it to, but I just cannot get the contents into my page. $file = file('http://MYDOMAIN/includes/FILE.txt'); //trim trailing whitespace from the list for ($i=0; $i<count($file); $i++) { $file[$i] = trim($file[$i]); } $arraycount = count($file); $new_array = array(); $randtestlist = array_rand($file, $arraycount); foreach ($randtestlist as $value) { array_push($new_array, $file[$value]); } $file = $new_array; $contents = "<div id='block1' class='maincontent'> $file "; Link to comment https://forums.phpfreaks.com/topic/69221-solved-problems-with-inserting-an-array/ Share on other sites More sharing options...
Jessica Posted September 13, 2007 Share Posted September 13, 2007 Well you never print $contents.... Link to comment https://forums.phpfreaks.com/topic/69221-solved-problems-with-inserting-an-array/#findComment-347904 Share on other sites More sharing options...
sunshine Posted September 13, 2007 Author Share Posted September 13, 2007 That's not it. The rest of my file is irrelevant, but is like this: include ("../templates/TEMPLATEFILE.php"); Then when a page is called such as www.MYDOMAIN.com/page.php, "page.php" contains the code mentioned above, such as $contents, then calls the TEMPLATEFILE and $contents gets put into the proper place on the page. Link to comment https://forums.phpfreaks.com/topic/69221-solved-problems-with-inserting-an-array/#findComment-347916 Share on other sites More sharing options...
sunshine Posted September 13, 2007 Author Share Posted September 13, 2007 One thing I'm experimenting with is this: //trim trailing whitespace from the list for ($i=0; $i<count($file); $i++) { $file[$i] = trim($file[$i]); } $arraycount = count($file); $new_array = array(); $randtestlist = array_rand($file, $arraycount); foreach ($randtestlist as $value) { array_push($new_array, $file[$value]); } $file = $new_array; function printTheList($printarray) { print $file; } $contents = "<div id='block1' class='maincontent'>". printTheList($printarray) ." </div> Not working. Trying all sorts of things. Can't get $file to output the array. Link to comment https://forums.phpfreaks.com/topic/69221-solved-problems-with-inserting-an-array/#findComment-347922 Share on other sites More sharing options...
Barand Posted September 13, 2007 Share Posted September 13, 2007 $contents = "<div id='block1' class='maincontent'>". printTheList($printarray) ."</div>"; In the above line you call the "printTheList() function passing $printarray as the argument - but $printarray has never been defined in the code. The array you want to print is $file, so pass that as the argument. $contents = "<div id='block1' class='maincontent'>". printTheList($file) ."</div>"; Note that because of the way you are using the function in the middle of a string, the function needs to return a string value. function printTheList($printarray) { print $file; } This function takes a single argument, $printarray, which is the on you want to print. It's no use saying "print $file" as the variable $file does not exist inside the function. You would need to print the argument passed to the function, viz. $printarray. However, as I said earlier, this function needs to return a string value, and not print an array, so function printTheList($printarray) { return join('<br />', $printarray); } Put it all together and you have <?php //trim trailing whitespace from the list for ($i=0; $i<count($file); $i++) { $file[$i] = trim($file[$i]); } $arraycount = count($file); $new_array = array(); $randtestlist = array_rand($file, $arraycount); foreach ($randtestlist as $value) { array_push($new_array, $file[$value]); } $file = $new_array; $contents = "<div id='block1' class='maincontent'>". printTheList($file) ."</div>"; function printTheList($printarray) { return join('<br />', $printarray); } ?> Link to comment https://forums.phpfreaks.com/topic/69221-solved-problems-with-inserting-an-array/#findComment-348005 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.