Jump to content

PHP Loop/Star Menu Help


Monkuar

Recommended Posts

Okay, I did this awhile ago but I did not do it appropiately, I used basic HTML AND it took forever.

 

I have 70 Icons, (They are all a Star icon).  If my user has donated $$ to the site, he will get his field name "max_stars" incremented to the appropiate value, (1-70) to his account, so he can have access to any of the stars and selects them in his profile settings menu to be activated and shown on his account Profile.

 

$i=1;
while($i<=70)
  {
  echo "<input type="radio" id="s1" value="1" name="star">IMG TO STAR HERE";
  $i++;
  }

 

 

Okay, so I want to loop through all 70 icons, each icon is named 1-70.png and each one has to correspond to there correct #, so it shows all 70 and each image, 1.png, 2.png/etc, correct incrimination.

 

I made the above code so far, but would like your help if you can help, I need to do all 70, but the radio boxes can only be enable to correspond to what the user's max_star field is in the  table.

 

So let's say user has max_stars set to 5.  That means only 1-5.png stars will have the radio box enabled, so they can click it, but still showing the other 65 as DISABLED, base don there max_star db field.

 

Then I would have to check if it's selected or not once they have updated there profile.  If somone can get me going here it would be great 

 

(I have the code to update there profile/etc, I just need help on showing the data in the loop) if not I would have to do this with 70 stupid radio input boxes and use 70 if else statements, it's just so annoying, I want to take advantage of PHP's foreach/looping.

.

 

Link to comment
Share on other sites

something like this maybe

$uservalue=5;
$i=1;
while($i<=70)
  {
  if ($i<=$uservalue) {  
    echo "<input type="radio" id="s1" value="$i" name="star">IMG TO STAR HERE";
  } else {
    echo "<input type="radio" id="s1" value="$i" name="star" disabled>IMG TO STAR HERE";
  }
  $i++;
  }

Link to comment
Share on other sites

code works great!

 

<?php
$uservalue=5;
$i=1;
while($i<=70)
  {
  if ($i<=$uservalue) {  
    echo '<input type="radio" id="s1" value="$i" name="star"><img src=img/icons/'.$i.'.png><br>';
  } else {
    echo '<input type="radio" id="s1" value="$i" name="star" disabled><img src=img/icons/'.$i.'.png><br>';
  }
  $i++;
  }
?>

 

Now I need help, how do I display them in 7columns? I wanna do 10 for each one,

 

here is my current screenshot:

 

66560151.jpg

 

Some are cut off, but yea :P

 

Link to comment
Share on other sites

maybe

<style type="text/css">
<!--
.column {
  position: relative;
  float: left;
  width: 30px;
}
-->
</style>
<?php
$uservalue=5;
$i=1;
while($i<=70)
  {
  if (in_array($i, array('1', '11', '21', '31', '41', '51', '61')) { echo "<div class=\"column\">"; }
  if ($i<=$uservalue) {  
    echo '<input type="radio" id="s1" value="$i" name="star"><img src=img/icons/'.$i.'.png><br>';
  } else {
    echo '<input type="radio" id="s1" value="$i" name="star" disabled><img src=img/icons/'.$i.'.png><br>';
  }
  if (in_array($i, array('10', '20', '30', '40', '50', '60', '70')) { echo "</div>"; }
  $i++;
  }
?>

Link to comment
Share on other sites

maybe

<style type="text/css">
<!--
.column {
  position: relative;
  float: left;
  width: 30px;
}
-->
</style>
<?php
$uservalue=5;
$i=1;
while($i<=70)
  {
  if (in_array($i, array('1', '11', '21', '31', '41', '51', '61')) { echo "<div class=\"column\">"; }
  if ($i<=$uservalue) {  
    echo '<input type="radio" id="s1" value="$i" name="star"><img src=img/icons/'.$i.'.png><br>';
  } else {
    echo '<input type="radio" id="s1" value="$i" name="star" disabled><img src=img/icons/'.$i.'.png><br>';
  }
  if (in_array($i, array('10', '20', '30', '40', '50', '60', '70')) { echo "</div>"; }
  $i++;
  }
?>

 

I'll try that if I can't get this one to work

 

$numCols = 6;
$numPerCol = ceil(70 / $numCols);
echo "<table><tr>";
for($col = 1; $col <= $numCols; $col++) {
   echo "<td><ul>";
   for($row = 0; $row < $numPerCol; $row++) {
      $resultRow = 70;
       if ($i<=$uservalue) { 
echo $row;	   
    echo '<input type="radio" id="s1" value="$i" name="star"><img src=img/icons/'.$row.'.png><br>';
  } else {
    echo '<input type="radio" id="s1" value="$i" name="star" disabled><img src=img/icons/'.$row.'.png><br>';
  }
   }
   echo "</ul></td>";
}
echo "</tr></table>";

 

Okay, I coded this but problem is:

 

ez2cd.jpg

 

I need it to display 1 through 70 on all of them,

 

any help?

 

Dragon_SA i'll try yours if I cant get this to work,

 

I also would need something to make the input radio box "selected" dependable on what they choose, it will be stored in the "stars" column any idea on that also?

Link to comment
Share on other sites

maybe

<style type="text/css">
<!--
.column {
  position: relative;
  float: left;
  width: 30px;
}
-->
</style>
<?php
$selected=3;
$uservalue=5;
$i=1;
while($i<=70)
  {
  if (in_array($i, array('1', '11', '21', '31', '41', '51', '61'))) { echo "<div class=\"column\">"; }
  if ($i == $selected) {
   echo "<input type=\"radio\" id=\"s1\" value=\"$\i" name=\"sta\r" selected><img src=\"img/icons/$i.png\"><br>";
  }
  if ($i<=$uservalue && $i != $selected) {  
    echo "<input type=\"radio\" id=\"s1\" value=\"$i\" name=\"star\"><img src=\"img/icons/$i.png\"><br>";
  } else {
    echo "<input type=\"radio\" id=\"s1\" value=\"$i\" name=\"star\" disabled><img src=\"img/icons/$i.png\"><br>";
  }
  if (in_array($i, array('10', '20', '30', '40', '50', '60', '70'))) { echo "</div>"; }
  $i++;
  }
?>

Link to comment
Share on other sites

maybe

<style type="text/css">
<!--
.column {
  position: relative;
  float: left;
  width: 30px;
}
-->
</style>
<?php
$selected=3;
$uservalue=5;
$i=1;
while($i<=70)
  {
  if (in_array($i, array('1', '11', '21', '31', '41', '51', '61')) { echo "<div class=\"column\">"; }
  if ($i == $selected) {
   echo '<input type="radio" id="s1" value="$i" name="star" selected><img src=img/icons/'.$i.'.png><br>';
  }
  if ($i<=$uservalue && $i != $selected) {  
    echo '<input type="radio" id="s1" value="$i" name="star"><img src=img/icons/'.$i.'.png><br>';
  } else {
    echo '<input type="radio" id="s1" value="$i" name="star" disabled><img src=img/icons/'.$i.'.png><br>';
  }
  if (in_array($i, array('10', '20', '30', '40', '50', '60', '70')) { echo "</div>"; }
  $i++;
  }
?>

 

 

syntax error, unexpected '{' in C:\wamp\www\s.php on line 1544

 

 

1544

 

  if (in_array($i, array('1', '11', '21', '31', '41', '51', '61')) { echo "<div class=\"column\">"; }

 

is it those {'s? how it causing a error lol

Link to comment
Share on other sites

set the width to 50px will give you more space in the div

 

or

 

if (in_array($i, array('1', '11', '21', '31', '41', '51', '61')) { echo "<td><ul>"; }

 

if (in_array($i, array('10', '20', '30', '40', '50', '60', '70'))) { echo "</ul></td>"; }

 

with the table tag above and end table below outside the while loop

Link to comment
Share on other sites

set the width to 50px will give you more space in the div

 

or

 

if (in_array($i, array('1', '11', '21', '31', '41', '51', '61')) { echo "<td><ul>"; }

 

if (in_array($i, array('10', '20', '30', '40', '50', '60', '70'))) { echo "</ul></td>"; }

 

with the table tag above and end table below outside the while loop

 

Topic Solved!!!

 

THANKS!

Link to comment
Share on other sites

also the value selected should be checked not selected sorry

 

  if ($i == $selected) {
   echo "<input type=\"radio\" id=\"s1\" value=\"$i\" name=\"star\" checked><img src=\"img/icons/$i.png\"><br>";
  }

 

LOL yea I changed that,

 

here is final result

 

cakex.jpg

 

ty <3

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.