1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Array arangement using loop

Discussion in 'Programming' started by neilfurry, Nov 7, 2017.

  1. #1
    Hi Mate,

    Im having problem displaying data from database using loop.

    i need to put all even numbers to the left and odd numbers to the right.

    so if i have one page it should display 2,1,4,3 instead of 1,2,3,4 and so on..

    attached is the output i want to come up.

    Cheers!

    Neil temp.png
    SEMrush
     
    Solved! View solution.
    neilfurry, Nov 7, 2017 IP
    SEMrush
  2. phpmillion

    phpmillion Member

    Messages:
    145
    Likes Received:
    11
    Best Answers:
    4
    Trophy Points:
    45
    #2
    Something like this in your loop will do the trick:

    if ($number % 2 == 0) {
      echo "<span class='pull-left'>$number</span>";
    }
    PHP:
    You will need to work with layout/CSS for yourself, but hope you get the main idea.
     
    phpmillion, Nov 8, 2017 IP
  3. saptarshi Rudra

    saptarshi Rudra Greenhorn

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #3
    Following could ve one solution to your problem.
    
    <html>
    <body>
    <?php
    $array=[1,2,3,4,5];
    echo '<div style="position: absolute;left: 2px; width: 250px;">';
    foreach($array as $value)
    {
    if($value%2==0)
    echo $value.'</br>';
    }
    echo '</div>';
    
    echo '<div style="position: absolute;left: 255px; width: 250px;">';
    foreach($array as $value)
    {
    if($value%2!=0)
    echo $value.'</br>';
    }
    echo '</div>';
    ?>
    </html>
    
    Code (markup):
    you can retrieve the even and odd values directly from database :
    here is the query:
    SELECT values FROM table where values %2=0;
    Capture.JPG
     
    Last edited by a moderator: Dec 17, 2017
    saptarshi Rudra, Dec 17, 2017 IP
  4. #4
    Output them in normal order in DIV with a wrapping DIV.

    <div class="reversed"><div>1</div><div>2</div><div>3</div><div>4</div></div>

    .reversed { position:relative; overflow:hidden; } /* make sure width is reported, wrap floats */
    .reversed div { float:right; width:50%; }

    done. You don't even need fancy logic server-side.

    Flex-box's row-reverse could also fix this, or the DIR attribute in the markup could also be used.

    <div dir="rtl"><div>12</div><div>34</div></div>

    Though beware UTF-8 encoding screws with that, so under UTF-8 use the reverse direction character instead of DIR... I forget offhand what character that is.

    -- edit -- forget that last part about rtl/ltr. I can't seem to get that working here which isn't ENTIRELY surprising. Bidirectional language support really shouldn't be used for this case anyways.
     
    Last edited: Jan 6, 2018
    deathshadow, Jan 6, 2018 IP