Page 1 of 1
Variable question
Posted: Fri Feb 03, 2006 12:14 pm
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.
Posted: Fri Feb 03, 2006 12:22 pm
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
Posted: Fri Feb 03, 2006 12:31 pm
by slash85
Thanks for the reply,
can you post a little example? I'm not the cleverest of chaps.
Thanks,
Slash.
Posted: Fri Feb 03, 2006 12:37 pm
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>
Posted: Fri Feb 03, 2006 12:38 pm
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>
Posted: Fri Feb 03, 2006 1:05 pm
by slash85
Thanks,
I will give this a whirl now,
Nice1,

Posted: Fri Feb 03, 2006 2:36 pm
by slash85
hawleyjr | Please use 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:
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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Fri Feb 03, 2006 2:39 pm
by raghavan20
you have to loop the results like ...
while($row = mysql_fetch_row($result))
and apply the logic inside the looping...
Posted: Wed Feb 08, 2006 11:56 am
by slash85
feyd | Please use 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
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Wed Feb 08, 2006 11:59 am
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.