Jump to content

[SOLVED] Problems With Inserting an Array


sunshine

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.