Jump to content

query results


searls03

Recommended Posts

quick question,

I have this code that returns over 100 buttons:

        <?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM products");
while($row = mysql_fetch_array($sql)){
$product = $row["product"];
$id =$row["id"];
$price =$row["price"];

?>
    <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

}
?>

What I would like is for the buttons to form columns of nine. ie 9 buttons in a column then a new column form....  how do I do this?

Link to comment
Share on other sites

need a little more info on how you need it formatted. do you just want that div duplicated 9 times? how exactly would you like them to be laid out in columns, ie one cell per button, or is a line break fine?

it's just formulaic html coding, you just have to find a piece of logic that explains properly what you want to achieve.

 

for example,

 

<?php $start = array ('1', '10', '19');
$end = array( '9', '18' )

$x = 1;
echo '<table><tr>';
foreach ($result as $button) {
if (in_array($x, $start) {
echo '<td>';
}
echo $button. '<br>';
if (in_array( $x, $end) {
echo '</td>';
}
?>

you can do with math, which may be better - but i haven't put much time into php math so i'm not sure how...

Link to comment
Share on other sites

linebreak.  if you are going to use a foreach loop....please help me with that.....I am not good at writing them.  so here is like a visual representation of what I want:

currently:

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

|

 

what I want:

|||||

|||||

|||||

|||||

|||||

|||||

|||||

|||||

|||||

 

does that make some more sense?

Link to comment
Share on other sites

ok...

 

foreach is a way of handling arrays in php. an array is a group of elements, so a foreach is saying

 

for EACH element AS $name, do this:

 

so

<?php 
$start = array ('1', '10', '19'); //columns will be started at each of these numbers
$end = array( '9', '18' ) // columns will be ended at each of these numbers

$x = 1; // set $x
echo '<table><tr>'; // start the table
foreach ($result as $button) { // i don't know the name of your array of buttons, i called it $result. this takes the array and processes each button according to what we have below.
if (in_array($x, $start) { // if x equals a number in the start array
echo '<td>';                   // start a column
}
echo $button. '<br>'; // output the button
if (in_array( $x, $end) { // if x equals a number in the end array 
echo '</td>'; // end the column
}
$X++; // increase $x by one
} // do it again. 
?>

the foreach will repeat until it has process each array element.

 

i forgot an essential part, $x ++; , which tells it to increase x by one. i added it here. i also forgot a bracket. sorry ;(

Link to comment
Share on other sites

this is what it is pulling from database.  I also need all the fields to move too with the buttons.

<?php
// Query member data from the database and ready it for display
$sql = mysql_query("SELECT * FROM products");
while($row = mysql_fetch_array($sql)){
$product = $row["product"];
$id =$row["id"];
$price =$row["price"];

?>
    <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

}
?>

Link to comment
Share on other sites

correct, you're fetching by row so the foreach won't work right, using your while loop you could do this:

 

<?php
// set layout vars
$x = 1; // set $x
$start = array ('1', '10', '19'); //columns will be started at each of these numbers
$end = array( '9', '18' ) // set numbers to end columns at
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");
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
echo '<br>';
} 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

?>

 

 

Link to comment
Share on other sites

do you mean random number of buttons in a column or random order of buttons ?

 

the start numbers must be the end numbers plus 1, it could be coded that way instead of having two arrays.

so you would change the end numbers to however many buttons you want in that column, and the start numbers to one after each of those (1 will always be a start number).

 

for random order of buttons, since you are doing it by row you can't do array_rand, you could add "ORDER BY RAND()" to your select statement. if you don't have many rows that's ok, but it's really not the way to do it. you want to try and not use ORDER BY RAND.

 

http://www.titov.net/2005/09/21/do-not-use-order-by-rand-or-how-to-get-random-rows-from-table/ this link explains an alternative.

 

or do i not understand you at all?

Link to comment
Share on other sites

yeah then you don't want in_array, you want to do the math for it.

 

here's an example from the man page for arithmetic operators:

 

<?php
if($n1 % $n2 == 0) echo "n1 is divisible by n2";
?>

because:

$a % $b Modulus Remainder of $a divided by $b.

if the remainder is 0, it divided evenly.

 

so in your code you could do:

 

<?php
// set layout vars
$x = 1; // set $x
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");
while($row = mysql_fetch_array($sql)){
$product = $row["product"];
$id =$row["id"];
$price =$row["price"];
if(9 % $x == 0) { // if 9 evenly divides by x
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
$end = $x + 1:
if(9 % $x !== 0) { // if 9 DOES NOT evenly divide by x
echo '<br>';
} else { // else if it DOES, 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(9 % $x !== 0) { // if 9 DOES NOT evenly divide by x - end the column as it wouldn't have ended itself
echo '</td>';
}
echo '</tr></table>'; // close the row and table

?>

 

past that you'd need to ask for math help, as i'm not well enough versed yet. i am interested in how well the above solution works though.

Link to comment
Share on other sites

maybe you can also help me with this.  what I need is for a loop to run to make tabbed pannels according to categories.  then I need the buttons to be put into the correct tab based on the category it falls under.  then a subcategory tabs inside the other tabs for products that have that specific sub category. 

 

here is what I sorta made....it doesn't work though....

 

 <div id="TabbedPanels1" class="TabbedPanels">
        <ul class="TabbedPanelsTabGroup"><?php $sql = mysql_query("SELECT * FROM categories");
while($row = mysql_fetch_array($sql)){
$category = $row["category"];
$sub =$row["subcategory"];
?>
          
<li class="TabbedPanelsTab" tabindex="0"><?php echo $category; ?></li>
<div class="TabbedPanelsContentGroup">
<div class="TabbedPanelsContent">
<?php
// set layout vars
$x = 1; // set $x
$start = array ('1', '10', '19', '28', '37', '46', '55', '64', '73', '82', '91', '100', '109', '118', '127', '135'); //columns will be started at each of these numbers
$end = array( '9', '18', '27', '36', '45', '54', '63', '72', '81', '90', '99','108','117', '126' ); // set numbers to end columns at
?>

        <?php echo '<table border="1px" cellpadding="0px" cellspacing="0px" ><tr height=\'50px\'>'; //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=".$category."");
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 width="100">';                   // start a column
} ?> 

     <?php $sql = mysql_query("SELECT * FROM categories");
while($row = mysql_fetch_array($sql)){
$category = $row["category"];
$sub =$row["subcategory"];
?>
          
<?php
}
?> 
      
       
    <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; padding: 0px; margin:0px; 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

?><?php
}
?>             </div>     

   </ul>
       
      </div>  

Link to comment
Share on other sites

sorry, that second to last one wasn't supposed to be here.....this is the code I meant to post in the third to last:

$x = 1; // set $x
for ($i=1; $i<=100; $i += 9)
  {  $start = array ($i);} //columns will be started at each of these numbers
for ($n=0; $n<=100; $n += 9)
{
$end = array($n);}

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.