Jump to content

take away "," in last loop


searls03

Recommended Posts

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?

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?

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

?>

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;

 

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.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.