searls03 Posted February 13, 2012 Share Posted February 13, 2012 how do I use this code and not add a "," after the last $i? $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { echo "'".$i."',";} you may change how you want.....I am pretty sure it needs a counter... but I don't know much about them Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/ Share on other sites More sharing options...
jcbones Posted February 13, 2012 Share Posted February 13, 2012 <?php $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { $count[] = "'{$i}'";} echo implode(', ',$count); Edit: didn't notice the single quotes. Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317430 Share on other sites More sharing options...
searls03 Posted February 13, 2012 Author Share Posted February 13, 2012 and how would I stick it into this code: $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { $count[] = "'{$i}'";} echo implode(', ',$count); // set layout vars $start = array ($stick in here); //columns will be started at each of these numbers where I want it to go inside where it says $stick in here? Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317439 Share on other sites More sharing options...
scootstah Posted February 13, 2012 Share Posted February 13, 2012 Huh? Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317447 Share on other sites More sharing options...
searls03 Posted February 13, 2012 Author Share Posted February 13, 2012 ok....the value that it echoing I would like to put into the place in the code where it says $stick in here Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317449 Share on other sites More sharing options...
scootstah Posted February 13, 2012 Share Posted February 13, 2012 Okay, but that doesn't really make sense based on your code/comment. $start = array (implode(', ', $count)); Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317451 Share on other sites More sharing options...
searls03 Posted February 13, 2012 Author Share Posted February 13, 2012 here is what I am trying to do....I am making columns of buttons, I need the buttons to automatically increment for me. This is what i have <?php $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { $count[] = "'{$i}'";} $start1 = implode(', ',$count); // set layout vars $x = 1; // set $x $start = array (implode(', ', $count)); //columns will be started at each of these numbers $end = array( '9', '18' ); // set numbers to end columns at echo $end; echo '<table><tr>'; //start the table, and the row. // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products where category='retail' and subcategory='none'"); while($row = mysql_fetch_array($sql)){ $product = $row["product"]; $id =$row["id"]; $price =$row["price"]; if (in_array($x, $start)) { // if x equals a number in the start array echo '<td>'; // start a column } ?> this is what I am trying to make using that code that makes numbers <?php $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { $count[] = "'{$i}'";} $start1 = implode(', ',$count); // set layout vars $x = 1; // set $x $start = array ('1', '10', '19', etc); //columns will be started at each of these numbers $end = array( '9', '18' ); // set numbers to end columns at echo $end; echo '<table><tr>'; //start the table, and the row. // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products where category='retail' and subcategory='none'"); while($row = mysql_fetch_array($sql)){ $product = $row["product"]; $id =$row["id"]; $price =$row["price"]; if (in_array($x, $start)) { // if x equals a number in the start array echo '<td>'; // start a column } ?> The code that i tried for dynamic(first code) does not work. how do I make it so that it duplicates what is in the second code? Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317454 Share on other sites More sharing options...
scootstah Posted February 13, 2012 Share Posted February 13, 2012 Okay, now I see what you're trying to do. You can simply do this: for ($i=1; $i<=100; $i += 9) { $start[] = $i; } Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317456 Share on other sites More sharing options...
searls03 Posted February 13, 2012 Author Share Posted February 13, 2012 one last thing, how would I make this display in rows instead of columns? <?php $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { $start[] = $i; } //columns will be started at each of these numbers for ($i=9; $i<=100; $i += 9) { $end[] = $i; } //columns wilecho $end; echo '<table><tr>'; //start the table, and the row. // Query member data from the database and ready it for display $sql = mysql_query("SELECT * FROM products where category='retail' and subcategory='none'"); while($row = mysql_fetch_array($sql)){ $product = $row["product"]; $id =$row["id"]; $price =$row["price"]; if (in_array($x, $start)) { // if x equals a number in the start array echo '<td>'; // start a column } ?> <div id="products"> <form action="" method="POST" name="myform<?php echo $id; ?>" class="myform<?php echo $id; ?>"> <input type="hidden" name="hiddenField" class="hiddenField" value="<?php echo $product; ?>" /> <input type="hidden" name="hiddenField2" class="hiddenField2" value="<?php echo $id; ?>" /> <input type="hidden" name="hiddenField1" class="hiddenField1" value="<?php echo $price; ?>" /> <input type="submit" name="submit" class="submit" value="<?php echo $product; ?>" style="background-color:lightgreen; height:50px; width:100px;"> </form> </div> <?php if (!in_array($x, $end)){ // if $x equals anything OTHER than 9, 18, etc - put in a line break } else { // else if it DOES equal 9, 18 ,etc end the column echo '</td>'; } $x ++; // increase x to keep count } // while loop ends //now outside of the loop, $x = $x - 1; //subtract the last $X++ so you know exactly how many buttons were added if (!in_array($x, $end)){ // if $x equals anything OTHER than 9, 18, etc - end the column as it wouldn't have ended itself echo '</td>'; } echo '</tr></table>'; // close the row and table ?> Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317458 Share on other sites More sharing options...
gizmola Posted February 13, 2012 Share Posted February 13, 2012 how do I use this code and not add a "," after the last $i? $x = 1; // set $x for ($i=1; $i { echo "'".$i."',";} you may change how you want.....I am pretty sure it needs a counter... but I don't know much about them I appear to be fairly late to the party, but I feel it's worth stating that a simple trick for this type of problem, is to simply rtrim off the last ','. to do this, you build the string in a variable: $str = ''; $x = 1; // set $x for ($i=1; $i $str .= "'$i',"; } $str = rtrim($str, ','); echo $str; Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317491 Share on other sites More sharing options...
scootstah Posted February 13, 2012 Share Posted February 13, 2012 how do I use this code and not add a "," after the last $i? $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { echo "'".$i."',";} you may change how you want.....I am pretty sure it needs a counter... but I don't know much about them I appear to be fairly late to the party, but I feel it's worth stating that a simple trick for this type of problem, is to simply rtrim off the last ','. to do this, you build the string in a variable: $str = ''; $x = 1; // set $x for ($i=1; $i<=100; $i += 9) { $str .= "'$i',"; } $str = rtrim($str, ','); echo $str; I sometimes do this too. However, I believe all he wanted was to populate an array. He thought that by adding the commas to a string, like "1, 2, 3" it would make this: array(1,2,3). searls03 by what I can gather it looks like you are trying to make a line break or new column or something every 9th row. If so, you are severely over-complicating it. You can use the modulus operator to do this. See the following example: for($i=1; $i<=100; $i++) { echo $i . ' '; if ($i % 9 == 0) echo '<br />'; } This will echo the numbers 1-100 and insert a line break every 9th number. Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317669 Share on other sites More sharing options...
searls03 Posted February 14, 2012 Author Share Posted February 14, 2012 scootsah, where exactly in my code would this go? Quote Link to comment https://forums.phpfreaks.com/topic/256994-take-away-in-last-loop/#findComment-1317989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.