Jump to content

multi Dimensonal Array!


Monkuar

Recommended Posts

$i=1;
$selected=0;
$uservalue=20;
$numCols = 3;
$numPerCol = ceil(20 / $numCols);
echo "<table class=tuble><tr>";
for($col = 1; $col <= $numCols; $col++) {
   echo "<td>";
   for($row = 0; $row < $numPerCol; $row++) {
      $resultRow = 20;
       if ($i<=$uservalue) {
if ($i == $selected) {
   $checked = 'checked';
  }else{
  $checked = '';
  }
$styles = array(

'Blue Essense' => array('#2B8EFF', '#63CBFF', '50 Forum Gold'),
'veggie' => array('carrot', 'collard', 'pea')
		  
		  
		  );
		  
		  
		  
    echo '<input type="radio" value="'.$i.'" name="form[color]">Blue Essence <b><a style="color:#2B8EFF;text-shadow:#63CBFF 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b><br>';
  }
  else {
    echo '<label for="s'.$i.'"><input type="radio" value="'.$i.'" name="form[star]" disabled><b><a href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b><br>';
  }
  $i++;
   }
   echo "</td>";
}
echo "</tr></table>";

 

Okay,  see the $styles array?

 

 $styles = array(

'Blue Essense' => array('#2B8EFF', '#63CBFF', '50 Forum Gold'),
'veggie' => array('carrot', 'collard', 'pea')
		  
		  
		  );

 

The first is hex code, background, and 2nd is #63CBFF (Front color) and the 3rd is how much it costs

 

I need those arrays to echo out correspondent with my

 

 echo '<input type="radio" value="'.$i.'" name="form[color]">Blue Essense<b><a style="color:#2B8EFF;text-shadow:#63CBFF 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (50 Forum Gold)<br>';

 

See how the style="color etc/etc" I need to grab the data from $styles to go into the correct spots, Is this feasable?

 

Long story short, I need to explode those arrays out somehow, any idea?

Link to comment
Share on other sites

Heres a massive updatE:

 

 

$i=1;
$selected=0;
$uservalue=20;
$numCols = 3;
$numPerCol = ceil(20 / $numCols);
echo "<table class=tuble><tr>";
for($col = 1; $col <= $numCols; $col++) {
   echo "<td>";
   for($row = 0; $row < $numPerCol; $row++) {
      $resultRow = 20;
       if ($i<=$uservalue) {
if ($i == $selected) {
   $checked = 'checked';
  }else{
  $checked = '';
  }
$styles = array(

'Blue Essense' => array('#2B8EFF', '#63CBFF', '50'),
'Pink Essence' => array('#FF08FF', '#E01DDA', '100'), 
'Green Essence' => array('#39FF08', '#54FF7C', '200'), 		
'Black Essense' => array('#000000', '#000000', '300'), 	  
'Pikachu' => array('#F4FF54', '#000000', '400'), 
'Charmander' => array('#FF0303', '#000000', '500'), 
'Ice Crystal' => array('#ffffff', '#00ccff', '600'), 
'Plated Steel' => array('#ffffff', '#363636', '700'), 
		  );
		  foreach ($styles as $value => $index) {

    echo '<input type="radio" value="'.$i.'" name="form[color]"><b><a style="color:'.$index[0].';text-shadow:'.$index[1].' 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (<b>'.$index[2].'</b> FG)<br>';
}
  }
  else {
    echo '<label for="s'.$i.'"><input type="radio" value="'.$i.'" name="form[star]" disabled><b><a href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b><br>';
  }
  $i++;
   }
   echo "</td>";
}
echo "</tr></table>";

 

Only 1 problem, it's showing like 150 of them:

 

2123nj.jpg

 

How can I retain my column rows, but have it correspond with my arrays?

Link to comment
Share on other sites

Your code is quite hard to read.  Instead of displaying your results in two columns, I'd first try to get the code to actually work before getting into all the fancy stuff.

 

One reason why your results keep getting repeated is that your $styles array is built inside of a for loop, then you have a foreach loop inside of that loop.  Loop-de-doop  :wtf:

 

Then after you get it to display the correct number of items, look for a better way to handle your columns.  I'm sure you'll figure it out, it's just all that messy redundant code is probably overwhelming you.

Link to comment
Share on other sites

Your code is quite hard to read.  Instead of displaying your results in two columns, I'd first try to get the code to actually work before getting into all the fancy stuff.

 

One reason why your results keep getting repeated is that your $styles array is built inside of a for loop, then you have a foreach loop inside of that loop.  Loop-de-doop  :wtf:

 

Then after you get it to display the correct number of items, look for a better way to handle your columns.  I'm sure you'll figure it out, it's just all that messy redundant code is probably overwhelming you.

 

I cleaned it up, and it's really close!

 

Problem is now, it's showing 2 rows of the same Data?!I Want to literally split it in half, so half of options on left column and other half on the right.

 

$i=1;
$selected=0;
$uservalue=20;
$numCols = 2;
$numPerCol = ceil(1 / $numCols);
echo "<table class=tuble><tr>";
for($col = 1; $col <= $numCols; $col++) {
   echo "<td>";
   for($row = 0; $row < $numPerCol; $row++) {
      $resultRow = 1;
       if ($i<=$uservalue) {
if ($i == $selected) {
   $checked = 'checked';
  }else{
  $checked = '';
  }
$styles = array(

'Purple Orb' => array('#660099', '#CC99FF', '20'),
'Ms Beautiful' => array('#FFFDB5', '#D391FF', '30'),
'Blue Essense' => array('#2B8EFF', '#63CBFF', '50'),
'Pink Essence' => array('#FF08FF', '#E01DDA', '100'), 
'Mythical Blue' => array('#0066ff', '#3300ff', '150'), 
'Green Essence' => array('#39FF08', '#54FF7C', '200'), 	
'Leaf Green' => array('#67e03f', '#000000', '250'), 		
'Black Essense' => array('#000000', '#000000', '300'), 	  
'Pikachu' => array('#F4FF54', '#000000', '400'), 
'Charmander' => array('#FF0303', '#000000', '500'), 
'White Purity' => array('#ffffff', '#ffffff', '520'),
'Pink Steel' => array('#ffffff', '#ff00ff', '540'), 
'Ice Crystal' => array('#ffffff', '#00ccff', '600'), 
'Plated Steel' => array('#ffffff', '#363636', '700'),
'Red Steel' => array('#ffffff', '#ff3333', '700'),
'The Dark Knight' => array('#000000', '#736f6e', '800'),
		  );

		  foreach ($styles as $value => $index) {

    echo ''.$value.'<input type="radio" value="'.$i.'" name="form[color]"><b><a style="color:'.$index[0].';text-shadow:'.$index[1].' 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (<b>'.$index[2].'</b> FG)<br>';
}
  }
  else {
    echo '<label for="s'.$i.'"><input type="radio" value="'.$i.'" name="form[star]" disabled><b><a href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b><br>';
  }
  $i++;
   }
   echo "</td>";
}
echo "</tr></table>";

 

Output:

 

ez25.jpg

Link to comment
Share on other sites

And I'll tell you how to fix it but like I said, you need a new way to handle your columns. 

 

If you read my first post you'd see that I said your $styles array is inside a for loop, that loops twice to display two colums.

hmmm, it loops around twice, and your getting two sets of data. 2x1 = 2. So it's no surprise to me that you're getting two sets of results.

 

If you end your for() loop bracket with } before the $styles array, you will not get two results of the same data.

Link to comment
Share on other sites

And I'll tell you how to fix it but like I said, you need a new way to handle your columns. 

 

If you read my first post you'd see that I said your $styles array is inside a for loop, that loops twice to display two colums.

hmmm, it loops around twice, and your getting two sets of data. 2x1 = 2. So it's no surprise to me that you're getting two sets of results.

 

If you end your for() loop bracket with } before the $styles array, you will not get two results of the same data.

 

Hey, Thanks for the response man.

 

  foreach ($styles as $value => $index) {
$value .= $value;
		  }
    echo ''.$value.'<input type="radio" value="'.$i.'" name="form[color]"><b><a style="color:'.$index[0].';text-shadow:'.$index[1].' 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (<b>'.$index[2].'</b> FG)<br>';

 

Okay, I tried this, it's working fine colums/etc, but it's only showing "The Dark Knight" my last array on my $styles multi demi array, isn'tt he .= supposed to xfer the arrays over?

 

Thanks if u can help

Link to comment
Share on other sites

Appending multiple values to a variable does work.

 

You aren't calling the code properly.

 

Notice, you are echoing  ONE radio, after all loops are closed, so you will always get the last result.

 

This is how the code should be:

  foreach ($styles as $value => $index) {
    echo ''.$value.'<input type="radio" value="'.$i.'" name="form[color]"><b><a style="color:'.$index[0].';text-shadow:'.$index[1].' 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (<b>'.$index[2].'</b> FG)<br>';
		  }

 

Now, if you don't necessarily want the html echo'd right there, you can append each input radio to a variable, by doing this:

 

  foreach ($styles as $value => $index) {
    $_values .= ''.$value.'<input type="radio" value="'.$i.'" name="form[color]"><b><a style="color:'.$index[0].';text-shadow:'.$index[1].' 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (<b>'.$index[2].'</b> FG)<br>';
		  }
echo $_values;

 

Link to comment
Share on other sites

Blah bro, I give up, I done a brand new code, and IT STILL SHOWS it wrong!

 

<table class=tuble>
<?php
$styles = array(

'Purple Orb' => array('#660099', '#CC99FF', '20.00'),
'Ms Beautiful' => array('#FFFDB5', '#D391FF', '30.00'),
'Blue Essense' => array('#2B8EFF', '#63CBFF', '50.00'),
'Pink Essence' => array('#FF08FF', '#E01DDA', '100.00'), 
'Mythical Blue' => array('#0066ff', '#3300ff', '150.00'), 
'Green Essence' => array('#39FF08', '#54FF7C', '200.00'), 	
'Leaf Green' => array('#67e03f', '#000000', '250.00'), 		
'Black Essense' => array('#000000', '#000000', '300.00'), 	  
'Pikachu' => array('#F4FF54', '#000000', '400.00'), 
'Charmander' => array('#FF0303', '#000000', '500.00'), 
'Purity' => array('#ffffff', '#ffffff', '520.00'),
'Pink Steel' => array('#ffffff', '#ff00ff', '540.00'), 
'Ice Crystal' => array('#ffffff', '#00ccff', '600.00'), 
'Plated Steel' => array('#ffffff', '#363636', '700.00'),
'Red Steel' => array('#ffffff', '#ff3333', '700.00'),
'The Dark Knight' => array('#000000', '#736f6e', '800.00'),
		  );
		  foreach ($styles as $value => $index) {
    $style .='<input type="radio" value="'.$i.'" name="form[color]"><b><a style="color:'.$index[0].';text-shadow:'.$index[1].' 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (<b>'.$index[2].'</b> FG)<br>';
}
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
       // open row if counter is zero
       if($i == 0)
          echo "<tr>";
   }
if($i < $max_columns)
{
    for($j=$i; $j<$max_columns;$j++)
        echo "<td>$style</td>";
}
?>
</tr>
</table>

 

Nicest code you're going to get, brand new

 

output:

 

 

ez21.jpg

 

It's working, but I don't want the data onto each new column... i want the data spread out, Can somone please help me? I seriously am stumped I have no idea what to do now.

 

It should be spread out evenly, 4 color's on each colum, not 16 on each....  :confused: :confused: :confused: :confused:

Link to comment
Share on other sites

Alright buddy, you've put in a good amount of effort which is what I like to see.

For your troubles and amount of time spent into getting your code to work, I'll finalize it for you so you can move on and not be held back by something so trivial.

Here you go, enjoy:

<table>
<?php
$styles = array(

'Purple Orb' => array('#660099', '#CC99FF', '20.00'),
'Ms Beautiful' => array('#FFFDB5', '#D391FF', '30.00'),
'Blue Essense' => array('#2B8EFF', '#63CBFF', '50.00'),
'Pink Essence' => array('#FF08FF', '#E01DDA', '100.00'), 
'Mythical Blue' => array('#0066ff', '#3300ff', '150.00'), 
'Green Essence' => array('#39FF08', '#54FF7C', '200.00'), 	
'Leaf Green' => array('#67e03f', '#000000', '250.00'), 		
'Black Essense' => array('#000000', '#000000', '300.00'), 	  
'Pikachu' => array('#F4FF54', '#000000', '400.00'), 
'Charmander' => array('#FF0303', '#000000', '500.00'), 
'Purity' => array('#ffffff', '#ffffff', '520.00'),
'Pink Steel' => array('#ffffff', '#ff00ff', '540.00'), 
'Ice Crystal' => array('#ffffff', '#00ccff', '600.00'), 
'Plated Steel' => array('#ffffff', '#363636', '700.00'),
'Red Steel' => array('#ffffff', '#ff3333', '700.00'),
'The Dark Knight' => array('#000000', '#736f6e', '800.00'),
		  );
$count = $idx = 0;
foreach ($styles as $value => $index) 
{
$style .= ( $count == 0 ) ? '<tr>' : '';
    $style .='<td><input type="radio" value="'.$i.'" name="form[color]"><b><a style="color:'.$index[0].';text-shadow:'.$index[1].' 2px 1px 1px" href="u.php?id='.$pun_user['id'].'">'.$pun_user['username'].'</a></b> (<b>'.$index[2].'</b> FG)</td>';
$idx++; $count++; 
if( ( $count == 2) || ( $index == end( $styles ) ) )
{ 
	$count = 0; 
}
}

$surplus = 2 - ( $idx - ( 2 * (ceil( $idx / 2 ) - 1) ) );
if( $idx > 2 )
{ 
for( $i = 1; $i <= $surplus; $i++ )
{ 
	$style .= '<td> </td>'; 
}
} 
$style .= '</tr>';

echo $style;
?>
</table>

 

Don't forget to mark this topic as solved, please and thank you.

Link to comment
Share on other sites

Wow, I guess the trouble all started when I started to use multi dimensional arrays and try to get them integrated into my forloops.

 

To be honest, your code will most likely be used on other stuff on my system aswell.

 

You pretty much just slammed with me more code then I could handle ( I would ask questions about it) but meh, i'll just try to study it overnight.

 

Topic Solved..  ;D

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.