SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP / DIV naming issue

    Howdy,

    I'm trying to write some PHP using some online examples and my output keeps coming out wrong. I think I'm slaughtering this elegant language. :O((

    What I'm trying to do is use PHP to frame an IMG tag with two DIVS. The PHP is intended to name the div's class with the same random numbers: either 101, 102, 103 or 104. I am also trying to add an 'a' or 'b' behind the DIV name. Here's my code, thus far:

    Code:
    <?php
    $rannumb1 = rand(101,104);
    $rannumb2 = $rannumba;
    print '<div class="$rannumb1a"><img src="temp_img.jpg" /><div class="$rannumb2b"></div></div>';
    ?>
    When I look at the output in IE, I see this:

    <div class="$rannumb1a"><img src="temp_img.jpg" /><div class="$rannumb2b"></div></div>

    I would like the same random number to appear and label the DIVs in pairs, like this:

    101a, 101b
    102a, 102b
    103a, 103b
    104a, 104b

    What I intend to do with this is use CSS to style a frame behind the image and another image on top of temp_img.jpg, as an overlay.

    My question is: how is this best done in PHP, so I can properly name those DIVs?

    Thanks!

  2. #2
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    using print with single quotes won't output the variables:

    instead of:

    print '<div class="$rannumb1a"><img src="temp_img.jpg" /><div class="$rannumb2b"></div></div>';

    use:

    print "<div class=\"$rannumb1a\"><img src=\"temp_img.jpg\" /><div class=\"$rannumb2b\"></div></div>";

    or even better:

    <div class="<? echo $rannumb1;?>a"><img src="temp_img.jpg" /><div class="<? echo $rannumb2;?>b"></div></div>';

  3. #3
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Adam,

    When I dropped that code into my PHP document:

    Code:
    <?php
    $rannumb1 = rand(101,104);
    $rannumb2 = $rannumba;
    <div class="<? echo $rannumb1;?>a"><img src="temp_img.jpg" /><div class="<? echo $rannumb2;?>b"></div></div>';
    ?>
    I get this error:

    Parse error: syntax error, unexpected '<' in /mydomain/public_html/testbed/index.php on line 24

    What did I do wrong?

    ...

    May I ask one more question?

    How would I modify the code if I want 4 total DIV-wrapped images? Would I use the same code four times? For instance:

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

  4. #4
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    like this

    PHP Code:
    <?php
    $rannumb1 
    rand(101,104);
    $rannumb2 $rannumba;
    ?>
    <div class="<?=$rannumb1;?>a"><img src="temp_img.jpg" /><div class="<?=$rannumb2;?>b"></div></div>

  5. #5
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by open4biz View Post
    How would I modify the code if I want 4 total DIV-wrapped images? Would I use the same code four times? For instance:

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

    <?php
    <div id="PHPHERE"><img src="temp_img.jpg"<div id="PHPHERE">
    ?>

    PHP Code:
    <?php
    for( $i=0$i<4$i++ ){
        
    $rannumb1 rand(101,104);
        
    $rannumb2 $rannumba;
    ?>
        <div class="<?=$rannumb1;?>a"><img src="temp_img.jpg" /><div class=" <?=$rannumb2;?>b"></div></div>
    <?php
    }
    ?>
    btw can't see where $rannumba is coming from.

    does it have to be random?

    PHP Code:
    <?php
    for( $i=0$i<4$i++ ){
    ?>
        <div class="<?=$i;?>a"><img src="temp_img.jpg" /><div class=" <?=$i;?>b"></div></div>
    <?php
    }
    ?>

  6. #6
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Adam,

    I used this bit of code:

    Code:
    <?php
    $rannumb1 = rand(101,104);
    $rannumb2 = $rannumb1;
    ?>
    <div class="<?=$rannumb1;?>a"><img src="images/tempimg.jpg" height="120" width="94" /><div class="<?=$rannumb2;?>b"></div></div>
    and it worked perfectly. Thank you!

    It looks like I will just repeat the code several times, because all 8 .jpg images will have different names.

    Thanks again!

  7. #7
    SitePoint Zealot adam.jimenez's Avatar
    Join Date
    May 2009
    Location
    Ware, UK
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by open4biz View Post
    Hi Adam,

    I used this bit of code:

    Code:
    <?php
    $rannumb1 = rand(101,104);
    $rannumb2 = $rannumb1;
    ?>
    <div class="<?=$rannumb1;?>a"><img src="images/tempimg.jpg" height="120" width="94" /><div class="<?=$rannumb2;?>b"></div></div>
    and it worked perfectly. Thank you!

    It looks like I will just repeat the code several times, because all 8 .jpg images will have different names.

    Thanks again!


    or put them in an array:


    PHP Code:
    <?php
    $images
    =array(
        
    'image1.jpg',
        
    'image2.jpg',
        
    'image3.jpg',
        
    'image4.jpg',
    );

    $i=101;
    foreach( 
    $images as $image ){
    ?>
        <div class="<?=$i;?>a"><img src="<?=$image;?>" /><div class="<?=$i;?>b"></div></div>
    <?php
        $i
    ++;
    }
    ?>

  8. #8
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dude!

    That works perfectly... except that it doesn't allow for the random numbers in the div tags' names. May I be a slight pain and ask how you would include that last little bit to tie it all together?

    I have to thank you. This is wonderful and you've really helped!

  9. #9
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    class names must start with a letter, underscore or dash.

    class="random-<?=$i;?>a"

  10. #10
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,015
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Be careful with short tags as not all servers support them. (I personally do use them but only because my framework manages an addendum to the httpd.conf file to function and I can force them to be available through the httpd.conf).

  11. #11
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I attempted to combine the two branches of Adam's help to create an ideal bit of PHP, but it's not parsing correctly. I am getting this error:

    Parse error: syntax error, unexpected $end in /mydomain/public_html/index.php on line 46

    Hash, thank you for the class comment. I modified the random value so a letter is first.

    Code:
    <?php
    $images=array(
        'image1.jpg',
        'image2.jpg',
        'image3.jpg',
        'image4.jpg',
    );
    $rannumb1 = rand(101,104);
    $rannumb2 = $rannumb1;
    foreach( $images as $image ){
    ?>
    <div class="a<?=$rannumb1;?>"><img src="<?=$image;?>" height="120" width="94" /><div class="b<?=$rannumb2;?>"></div></div> (this is line 46)
    How do I need to modify the PHP to include the random variables in the array each time?

    A thank you to all for your help!

  12. #12
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Michael Morris View Post
    Be careful with short tags as not all servers support them. (I personally do use them but only because my framework manages an addendum to the httpd.conf file to function and I can force them to be available through the httpd.conf).
    Hi Michael,

    I have no idea what this means. Could you explain a little more?

    Thanks!

  13. #13
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Parse error: syntax error, unexpected $end in /mydomain/public_html/index.php on line 46
    You opened a foreach loop but didn't close it

    Short tags: <? ?>
    Standard tags: <?php ?>

    The problem is that the server has to be specifically set up to interpret the first as php, where as the second will always be run as php.
    Probably best to use either of these when mixing html and php:
    HTML Code:
    <div class="<?php echo $class; ?>">stuff</div>
    PHP Code:
    echo '<div class="'.$class.'">stuff</div>';
    // you can use double quotes to have php parse variables in strings, but I think it's harder to read
    // read up on strings http://nz.php.net/manual/en/language.types.string.php
    echo "<div class=\"$class\">stuff</div>"
    Depends mostly on preference and readability, ie mostly php then use second, mostly html? first.

    How do I need to modify the PHP to include the random variables in the array each time?
    Not sure what you mean there. You want to display 4 images, each different, and each with a style chosen randomly form a set of 4?
    PHP Code:
    $classes = array(
        
    'funky',
        
    'sombre',
        
    'blue'
        
    'green');
    $images = array( ... ); // as above

    foreach($images as $i) {
        
    $c $classes[mt_rand(03)];
        echo 
    '<img src="'.$i.'" class="'.$c.'">';

    Using divs may not be necessary if you style the image instead.

  14. #14
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,015
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by open4biz View Post
    Hi Michael,

    I have no idea what this means. Could you explain a little more?

    Thanks!
    <? ?> and <?= ?> are PHP's "short tags". The full length tags are <?php ?> and <script type="php"> </script> (rarely used). It is possible to disable short tags in the php.ini file though they can be re-enabled through httpd.conf or .htaccess directives. Therefore don't get *too* used to using them lest you find yourself in an environment where they aren't allowed.

  15. #15
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hash,

    Thank you for using mt_rand. I read up on it and it would seem that's the superior randomizer code to use. Anyhoo, I tried really hard to implement your code by splicing it all together:

    Code:
    <?php
    $images=array(
        'image1.jpg',
        'image2.jpg',
        'image3.jpg',
        'image4.jpg',
    );
    foreach($images as $i) {
    $frm = $mt_rand(101,104);
    echo '<div class="a'.$frm.'"><img src="'.$i.'" alt="" height="120" width="94" /><div class="b'.$frm.'"></div></div>';
    }
    It seems like it should work, but it just isn't.

    The DIV's are necessary as I'm sandwiching an image behind and in front of the <img> tag. I'm trying to name the DIV's with the following convention:

    a101, b101
    a102, b102
    a103, b103
    a104, b104

    This is to differentiate them from another class I have buried in the same document (1,2,3,4).

    I feel like it's soooooo close.

  16. #16
    SitePoint Enthusiast open4biz's Avatar
    Join Date
    Jan 2004
    Location
    San Diego
    Posts
    87
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Holy cow. I got it.

    Code:
    <?php
    $images=array(
        'image1.jpg',
        'image2.jpg',
        'image3.jpg',
        'image4.jpg',
    );
    $rannumb1 = rand(101,104);
    $rannumb2 = $rannumb1;
    foreach( $images as $image ){
    ?>
    <div class="a<?=$rannumb1;?>"><img src="<?=$image;?>" alt="" height="120" width="94" /><div class="b<?=$rannumb2;?>"></div></div>
    <?php
    $rannumb1 = rand(101,104);
    $rannumb2 = $rannumb1;
    }
    ?>
    Hey, it only took the whole day!! LOL

    Thank you, thank you, thank you to all who helped. I really appreciate it!! Adam, Hash - you two rock!

  17. #17
    I solve practical problems. bronze trophy
    Michael Morris's Avatar
    Join Date
    Jan 2008
    Location
    Knoxville TN
    Posts
    2,015
    Mentioned
    62 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by open4biz View Post
    Holy cow. I got it.

    PHP Code:
    <?php
    $images
    =array(
        
    'image1.jpg',
        
    'image2.jpg',
        
    'image3.jpg',
        
    'image4.jpg',
    );
    $rannumb1 rand(101,104);
    $rannumb2 $rannumb1;
    foreach( 
    $images as $image ){
    ?>
    <div class="a<?=$rannumb1;?>"><img src="<?=$image;?>" alt="" height="120" width="94" /><div class="b<?=$rannumb2;?>"></div></div>
    <?php
    $rannumb1 
    rand(101,104);
    $rannumb2 $rannumb1;
    }
    ?>
    Hey, it only took the whole day!! LOL

    Thank you, thank you, thank you to all who helped. I really appreciate it!! Adam, Hash - you two rock!
    Are you sure you want the rows to be randomly labeled? And if so why not have the images randomized? Also do you need a particular class to be associated with a particular image? If so then an associative array is called for. Even if not associative you don't need to use a random number or a counter to vary the rows as follows.

    PHP Code:
    <?
    $images 
    = array(
        
    'image1.jpg',
        
    'image2.jpg',
        
    'image3.jpg',
        
    'image4.jpg',
    );

    /* If you want the order of the images above
     * to be random then shuffle the array.
     */
    shuffle($images);

    /* The following is "braceless syntax" It is better than
     * standard syntax in situations where PHP code and
     * HTML are heavily intermingled since braces can
     * be easily lost among the tags making the code
     * hard to read
     */
    foreach( $images as $key => $image ): ?>
        <div class="a<?= $key 101 ?>">
            <img src="<?= $image ?>" alt="" height="120" width="94" />
            <div class="b<?= $key 101 ?>">
            </div>
        </div>
    <? /* The following statement closes a
    foreach loop in braceless syntax */ 
    ?>

    <? endforeach ?>
    You'll notice some missing semicolons above. This is allowed since a closing ?> tag will act as a line terminating semicolon - the omission hence making it easier to read.

    Note on arrays - even if you don't explicitly assign a key to each value PHP will do so, indexing from 0. Hence

    PHP Code:
    $images = array(
        
    'image1.jpg',
        
    'image2.jpg',
        
    'image3.jpg',
        
    'image4.jpg',
    ); 
    is the same as

    PHP Code:
    $images = array(
        
    => 'image1.jpg',
        
    => 'image2.jpg',
        
    => 'image3.jpg',
        
    => 'image4.jpg',
    ); 
    But if you need css class names to be associated with the images you can name the keys.

    PHP Code:
    $images = array(
        
    'class1' => 'image1.jpg',
        
    'class2' => 'image2.jpg',
        
    'class3' => 'image3.jpg',
        
    'class4' => 'image4.jpg',
    ); 
    Keys must be unique. Warning - if they aren't PHP will only pitch a notice level error before changing the value assigned to the redundant key - and on many setups you won't even see that error since notices level errors are suppressed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •