Variable question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
slash85
Forum Newbie
Posts: 18
Joined: Fri Feb 03, 2006 12:02 pm

Variable question

Post by slash85 »

Hi,

I have a txt file that i read into a variable, call it $data for now. When i echo the variable it echo's like this:

1
2
3
4
5
6
7
8

Which is what i would expect, my question is can i re-direct say half of the results? Instead of displaying 1-8 down the page can i display the results like this?

1 5
2 6
3 7
4 8

Any help please?

Thanks,
Slash.
Netfeed
Forum Newbie
Posts: 1
Joined: Wed Dec 15, 2004 8:05 am
Location: Upon a hill where the grass is green
Contact:

Post by Netfeed »

this isnt really that thought thrue from my side but why not:

save the data in a array
get the number of items of the array and then split them in two
echo array[0] and array[number_of_items/2]
echo array[1] and array[number_of_items/2+1]
echo array[2] and array[number_of_items/2+2]
and so on for until n == number_of_items/2

wouldnt that do? :)

edit: yay, my first post
slash85
Forum Newbie
Posts: 18
Joined: Fri Feb 03, 2006 12:02 pm

Post by slash85 »

Thanks for the reply,

can you post a little example? I'm not the cleverest of chaps.

Thanks,
Slash.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Code: Select all

<style>
span{
	width:90px; background-color:#00FF00; margin-bottom:2px;
}

div{	
	background-color:#000000; border:2px solid #000000; color:red; width:100px;
}
</style>

<pre>

<?php 
displayInHalf(35);

displayInHalf(20);

function displayInHalf($maxCount){
	//echo $maxCount/2;
	//echo "<div>";
	for ($i = 1; $i <= $maxCount; $i++){
		if ($i <= (ceil($maxCount/2))){
			if ($i == 1){
				echo "<div style = 'float:left;'><span>$i</span><br />";
			}else{
				echo "<span>$i</span><br />";
			}	
	
		}else{
			if ($i == ceil($maxCount/2) + 1){
				echo "</div><div><span>$i</span><br />";
			}elseif ($i == $maxCount){
				echo "<span>$i</span><br /></div>";
			}
			else{
				echo "<span>$i</span><br />";
			}
		}
	}
	//echo "</div>";
}
?>  
</pre>
Last edited by raghavan20 on Fri Feb 03, 2006 1:12 pm, edited 3 times in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Here is another example:

Code: Select all

//untested

	$array_cnt = count( $a_ );
	//1/2 mark
	$half = $array_cnt / 2;
	
	echo '<div style="float: left;">';
	
	for( $i=0; $i<$array_cnt; $i++ ){
		
		if( $i == $half )
			echo '</div><div>';
		
	
		echo $a_[$i] . '<br />';
	
	}
	
	echo '</div>';
Should output something like this:

Code: Select all

<div style="float: left;">1<br />
    2<br />
    3<br />
    4<br />
  </div><div>5<br />
    6<br />
    7<br />
    8<br />
  </div>
slash85
Forum Newbie
Posts: 18
Joined: Fri Feb 03, 2006 12:02 pm

Post by slash85 »

Thanks,

I will give this a whirl now,

Nice1,

:lol:
slash85
Forum Newbie
Posts: 18
Joined: Fri Feb 03, 2006 12:02 pm

Post by slash85 »

hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,

I have done as hawleyjr stated, and in a test in its simplest form it works, nice1.  I thort from that i would beable 2 have a little play and get it to work the actual way i want.  What i'm doing is pulling the data from mysql using mysql_fetch array.  Like this:

Code: Select all

$result = mysql_query("SELECT * FROM images where image_id = '2'");

$row = mysql_fetch_array($result);

echo "<IMG BORDER=\"0\" SRC=\"$row[2]\"><br>";
When i put $row[2] into the array:

Code: Select all

$a_ = array($row[2]);
All that it echo's is the last result in the array and not the whole list.

Is ther a way i can get $row[2] to keep all its data??

Thanks in advance

Slash.


hawleyjr | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

you have to loop the results like ...
while($row = mysql_fetch_row($result))

and apply the logic inside the looping...
slash85
Forum Newbie
Posts: 18
Joined: Fri Feb 03, 2006 12:02 pm

Post by slash85 »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Ok,

So i've been trying to solve this on and off for a couple of days now.  Any more help or suggestions.

I'm trying to echo out a <a href> that contains the link and the image that i want, which works.  But i cant figure out for the life of me how to split the results, i've tried some great help from above but i dont think i can use <div> inside <a href>

Code: Select all

<?

$result = mysql_query("SELECT * FROM test where album_id = '2'");
$result2 = mysql_query("SELECT * FROM images where album_id = '2'");

 while($row = mysql_fetch_array($result)) {
  echo "<a href=\"$row[3]$row[1]\n\">";
  
 while($row2 = mysql_fetch_array($result2)) { 

  echo"<IMG BORDER=\"0\" SRC=\"$row2[2]\"><br>";

 echo "</a>";

?>


any one got a idea how i can echo it like this:

Code: Select all

Link1 <image1>       Link4 <image4>
Link2 <image2>       Link5 <image5>
Link3 <image3>       Link6 <image6>
Insted of:

Code: Select all

Link1 <image1> 
Link2 <image2> 
Link3 <image3>
Link4 <image4>
Link5 <image5>
Link6 <image6>
Thanks again for any help,

Slash.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I've posted about that form of display on several occasions, you can find links to a few of those threads via the Forum Tour link in my signature, found through the Useful Posts thread it links to.
Post Reply