Jump to content

how cant i sort an array in to div in pairs (odd and even) ?


ricky-spires

Recommended Posts

hello

 

how cant i sort an array in to div in pairs (odd and even) ?

 

so i have the numbers 1 to 50 for a book

 

i want the first number and last number to be front and back pages.

 

ie.

 

1

2+3

3+4

4+5

5+6

7

 

this is the div structure

 

im giving the front cover a class of fCover then left for the left pages and right for the right pages.

<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
                
        //FRONT PAGE      
	<div class="bb-item" id="item1">
	    <div class="content">
		<div class="scroller fCover">
		<img src="assets/images/pages/1.jpg" ALT="page1">
		</div>
	    </div>
	</div>
                    
                    
	//PAGES 2 & 3
        <div class="bb-item" id="item2">
            <div class="content">

                //PAGE 2
		<div class="scroller left">
		     <img src="assets/images/pages/2.jpg" ALT="page2">
		</div>

                //PAGE 3
                <div class="scroller right">
		     <img src="assets/images/pages/3.jpg" ALT="page3">
		</div>
	    
            </div>
	</div>

</div>
</div>

This is the php that i have:

<div class="bb-custom-wrapper">
<div id="bb-bookblock" class="bb-bookblock">
                
   <?php
	foreach($pageImg as $file){
	echo'
	<div class="bb-item" id="item'.$file.'">
		<div class="content">';
						
		if($file == "1"){
		echo'
		<div class="scroller fCover">
		<img src="assets/images/pages/'.$file.'.jpg" ALT="page'.$file.'">
		</div>';
		}
		echo'	
		</div>
	</div>';
	}?>
                    
</div></div>

that give me the first page but im stuck on the paired pages ?

 

thanks

rick

Edited by ricky-spires
Link to comment
Share on other sites

You can loop through your pages and check the page number:

if ($page_number&1)
	// $page_number is odd
else
	// $page_number is even

The last digit in a binary string will alternate between 0 and 1 as it is incremented, so you can use a "bitwise and" against the number 1 to check if it is odd or even.

Link to comment
Share on other sites

this is another way i tried it but it doesnt pair them together.

foreach($pageImg as $file){
					
					if($file == "1"){
					
					echo'
					<div class="bb-item" id="item'.$file.'">
						<div class="content">
						<div class="scroller fCover">
								<img src="assets/images/pages/'.$file.'.jpg" ALT="page'.$file.'">
							</div>
						</div>
					</div>';
					
					}else{
					
						echo'
						<div class="bb-item" id="item'.$file.'">
						<div class="content">
						<div class="scroller ';
						
						if ($file % 2 == 0) { 
							echo 'right'; 
						} else { 
							echo 'left';
						}
						echo' "><img src="assets/images/pages/'.$file.'.jpg" ALT="page'.$file.'">
						</div>
						</div>
					</div>';
						
					}
					
				}
Link to comment
Share on other sites

thanks lemmin,

 

but thats doesnt solve the grouping issue im having

 

say i have numbers 1,2,3,4,5,6,7,8,9,10

 

how do i put pair in to a div.

 

so

 

<div>

image 1 class="left"

image 2 class="right"

</div>

 

<div>

image 3 class="left"

image 4 class="right"

</div>

 

<div>

image 5 class="left"

image 6 class="right"

</div>

 

 

 

 

 

 

 

all im getting now is

 

 

<div>

image 1 class="left"

</div>

 

<div>

image 2 class="right"

</div>

 

<div>

image 3 class="left"

</div>

 

<div>

image 4 class="right"

</div>

Edited by ricky-spires
Link to comment
Share on other sites

How about this:

$pageImg = array(1,2,3,4,5,6,7,8,9,10,11,12);
$n = sizeof($pageImg)-1;

echo '<div class="firstPage">Image '.$pageImg[0].'</div>';

for ($i=1;$i<$n;$i+=2)
{
	echo
	'<div>
		Image '.$pageImg[$i].' class="left"<br/>
		Image '.$pageImg[$i+1].' class="right"
	</div>';
}

if (!($pageImg[$n] % 2))
	echo '<div class="lastPage">Image '.$pageImg[$n].'</div>';
Link to comment
Share on other sites

ok. im getting there.

 

this is what i get when i use fire bug.  the problem now is that its skipping the first left.

 

<div id="item2" class="bb-item">
<div class="content">
<div class="scroller ">
<div class="scroller right">
</div>
</div>
<div id="item3" class="bb-item">
<div class="content">
<div class="scroller left">
<div class="scroller right">
</div>
</div>
<div id="item4" class="bb-item">
<div class="content">
<div class="scroller left">
<div class="scroller right">
</div>
</div>

 

 

 

this is the code i used:

<?php
			
				foreach($pageImg as $file){
					
					if($file == "1"){
					
					echo'
					<div class="bb-item" id="item'.$file.'">
						<div class="content">
						<div class="scroller fCover">
								<img src="assets/images/pages/'.$file.'.jpg" ALT="page'.$file.'">
							</div>
						</div>
					</div>';
					
					}else{
					
						echo'
						<div class="bb-item" id="item'.$file.'">
						<div class="content">';
						
						if($file % 2 != 0){ $left = "left"; }
						if($file % 2 == 0){ $right = "right"; }	
						
						echo'	
						<div class="scroller '.$left.'"><img src="assets/images/pages/'.$file.'.jpg" ALT="page'.$file.'">
						</div>
						
						<div class="scroller '.$right.'"><img src="assets/images/pages/'.$file.'.jpg" ALT="page'.$file.'">
						</div>
						
						
						</div>
					</div>';
						
					}
					
				}
				
				?>

SCRAP THAT

 

its putting the same image in the divs

 

<div id="item2" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page2" src="assets/images/pages/2.jpg">
</div>
<div class="scroller ">
<img alt="page2" src="assets/images/pages/2.jpg">
</div>
</div>
</div>
<div id="item3" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page3" src="assets/images/pages/3.jpg">
</div>
<div class="scroller right">
<img alt="page3" src="assets/images/pages/3.jpg">
</div>
</div>
</div>

 

 

 

let me try your code. thanks

Edited by ricky-spires
Link to comment
Share on other sites

Hello lemmin

 

thats great. THANKS

 

just 1 more thing.. they seem to start at the number 10

 

after the first page which is the cover it need to start from the number 2 (1 being the cover)

 

 

this is what im getting

 

<div id="item10" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page10" src="assets/images/pages/10.jpg">
</div>
<div class="scroller right">
<img alt="page11" src="assets/images/pages/11.jpg">
</div>
</div>
</div>
<div id="item11" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page11" src="assets/images/pages/11.jpg">
</div>
<div class="scroller right">
<img alt="page12" src="assets/images/pages/12.jpg">
</div>
</div>
</div>

 

 

 

this is the code:

<div class="bb-custom-wrapper">
				<div id="bb-bookblock" class="bb-bookblock">
                
					<?php
                    $n = sizeof($pageImg)-1;
                    
                    echo '
                    <div class="bb-item" id="item'.$pageImg[0].'">
                        <div class="content">
                        	<div class="scroller fCover">
                                <img src="assets/images/pages/'.$pageImg[0].'.jpg" ALT="page'.$pageImg[0].'">
                            </div>
                        </div>
                    </div>';
                    
                    for ($i=1;$i<$n;$i+=1)
                    {
                        echo'
                        <div class="bb-item" id="item'.$pageImg[$i].'">
							<div class="content">
								<div class="scroller left"><img src="assets/images/pages/'.$pageImg[$i].'.jpg" ALT="page'.$pageImg[$i].'"></div>
								<div class="scroller right"><img src="assets/images/pages/'.$pageImg[$i+1].'.jpg" ALT="page'.$pageImg[$i+1].'"></div>
							</div>
						</div>';
                    }
                    /*
                    if (!($pageImg[$n] % 2)){
                        echo '<div class="lastPage">Image '.$pageImg[$n].'</div>';
                    }
                    */
                    ?>
                </div>
            </div>

thanks so much

 

ricky

Link to comment
Share on other sites

getting there

 

<div id="item1" class="bb-item">

<div class="content">
<div class="scroller left">
<img alt="page1" src="assets/images/pages/1.jpg">
</div>
<div class="scroller right">
<img alt="page2" src="assets/images/pages/2.jpg">
</div>
</div>
</div>
<div id="item2" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page2" src="assets/images/pages/2.jpg">
</div>
<div class="scroller right">
<img alt="page3" src="assets/images/pages/3.jpg">
</div>
</div>
</div>

 

i did this at the start:

sort($pageImg);
  $n = sizeof($pageImg);

now.. how do i start from the number 2 and not 1 ?

Link to comment
Share on other sites

hello.

 

the items are sorted and incrementing correctly which is great

 

<div id="item1" class="bb-item">

<div id="item2" class="bb-item">
<div id="item3" class="bb-item">
<div id="item4" class="bb-item">
<div id="item5" class="bb-item">
<div id="item6" class="bb-item">
<div id="item7" class="bb-item">

 

 

but the page number are starting from the wrong number:  the last ans first are the same:

 

 

<div id="item1" class="bb-item">

<div class="content">
<div class="scroller fCover">
<img alt="page1" src="assets/images/pages/1.jpg">
</div>
</div>
</div>
<div id="item2" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page2" src="assets/images/pages/2.jpg">
</div>
<div class="scroller right">
<img alt="page3" src="assets/images/pages/3.jpg">
</div>
</div>
</div>
<div id="item3" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page3" src="assets/images/pages/3.jpg">
</div>
<div class="scroller right">
<img alt="page4" src="assets/images/pages/4.jpg">
</div>
</div>
</div>
<div id="item4" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page4" src="assets/images/pages/4.jpg">
</div>
<div class="scroller right">
<img alt="page5" src="assets/images/pages/5.jpg">
</div>
</div>
</div>

 

 

 

i noticed that if i change

for ($i=1;$i<$n;$i+=1)

to

for ($i=1;$i<$n;$i+=2)

it fixes the problem but the the item number go up in event numbers .  ie... 2,4,6 etc..

 

how can i seperate them

 

 

 

this is the code so far. i removed some divs to make it easier.

sort($pageImg);
                    $n = sizeof($pageImg)-1;
                    
                    echo '
					
							<div class="bb-item" id="item'.$pageImg[0].'">
								<div class="content">
									<div class="scroller fCover">
										<img src="assets/images/pages/'.$pageImg[0].'.jpg" ALT="page'.$pageImg[0].'">
									</div>
								</div>
							</div>
						';
					
					
                    for ($i=1;$i<$n;$i+=1)
                    {
                        echo'
					
								<div class="bb-item" id="item'.$pageImg[$i].'">
									<div class="content">
									
											<div class="scroller left"><img src="assets/images/pages/'.$pageImg[$i].'.jpg" ALT="page'.$pageImg[$i].'"></div>
											<div class="scroller right"><img src="assets/images/pages/'.$pageImg[$i+1].'.jpg" ALT="page'.$pageImg[$i+1].'"></div>
										
									</div>
								</div>
							';
                    }

thanks

Link to comment
Share on other sites

I'm not sure how you want the item numbers to relate. If you want the item numbers to increment separately, you will need a separate counter:

$j=1;
for ($i=1;$i<$n;$i+=2)
{
	echo'

			<div class="bb-item" id="item'.$j.'">
				<div class="content">
				
						<div class="scroller left"><img src="assets/images/pages/'.$pageImg[$i].'.jpg" ALT="page'.$pageImg[$i].'"></div>
						<div class="scroller right"><img src="assets/images/pages/'.$pageImg[$i+1].'.jpg" ALT="page'.$pageImg[$i+1].'"></div>
					
				</div>
			</div>
		';
		$j++;
}
Link to comment
Share on other sites

 

<div id="item1" class="bb-item">

<div class="content">
<div class="scroller fCover">
<img alt="page1" src="assets/images/pages/1.jpg">
</div>
</div>
</div>
<div id="item2" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page2" src="assets/images/pages/2.jpg">
</div>
<div class="scroller right">
<img alt="page3" src="assets/images/pages/3.jpg">
</div>
</div>
</div>
<div id="item2" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page4" src="assets/images/pages/4.jpg">
</div>
<div class="scroller right">
<img alt="page5" src="assets/images/pages/5.jpg">
</div>
</div>
</div>

 

I THOUGHT THAT WAS IT.

 

all the items are getting the number 2. they should also go up (1,2,3,4 etc)

 

 

$j=1;
                    for ($i=1;$i<$n;$i+=2)
                    {
                        echo'
					
								<div class="bb-item" id="item'.$pageImg[$j].'">
									<div class="content">
									
											<div class="scroller left"><img src="assets/images/pages/'.$pageImg[$i].'.jpg" ALT="page'.$pageImg[$i].'"></div>
											<div class="scroller right"><img src="assets/images/pages/'.$pageImg[$i+1].'.jpg" ALT="page'.$pageImg[$i+1].'"></div>
										
									</div>
								</div>
							';
                    }
Edited by ricky-spires
Link to comment
Share on other sites

ok.. :) i missed a bit

 

now im getting

 

 

<div id="item1" class="bb-item">

<div id="item1" class="bb-item">
<div id="item2" class="bb-item">
<div id="item3" class="bb-item">
<div id="item4" class="bb-item">
<div id="item5" class="bb-item">
<div id="item6" class="bb-item">
<div id="item7" class="bb-item">
<div id="item8" class="bb-item">
<div id="item9" class="bb-item">
<div id="item10" class="bb-item">

 

 

 

just need to skip the first number :)

Link to comment
Share on other sites

DONE IT

 

<div class="menu-panel">

<div id="item1" class="bb-item">
<div class="content">
<div class="scroller fCover">
<img alt="page1" src="assets/images/pages/1.jpg">
</div>
</div>
</div>
<div id="item2" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page2" src="assets/images/pages/2.jpg">
</div>
<div class="scroller right">
<img alt="page3" src="assets/images/pages/3.jpg">
</div>
</div>
</div>
<div id="item3" class="bb-item">
<div class="content">
<div class="scroller left">
<img alt="page4" src="assets/images/pages/4.jpg">
</div>
<div class="scroller right">
<img alt="page5" src="assets/images/pages/5.jpg">
</div>
</div>
</div>

 

 

just need to do $j=2;

 

 

THANKS FOR ALL YOUR HELP

 

i have learnt alot today :)

 

 

rick

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.