searls03 Posted February 4, 2012 Share Posted February 4, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/ Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314471 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314473 Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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 ;( Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314483 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 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 } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314487 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 I think I am running into the problem because they are not array....they are simply a looped result. Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314491 Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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 ?> Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314495 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 how would I make the start numbers dynamic? Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314528 Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314533 Share on other sites More sharing options...
searls03 Posted February 4, 2012 Author Share Posted February 4, 2012 no, I need them to automatically fill in those numbers for like when more buttons are added. otherwise I have to manually key them in. so like $start = array (Automatically increment); Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314542 Share on other sites More sharing options...
digibucc Posted February 4, 2012 Share Posted February 4, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314547 Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 how would I make a method similar to this work? this is where += is from http://php.net/manual/en/language.operators.increment.php Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314610 Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 also I would like the buttons to touch. Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314615 Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 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> Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314618 Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 anything? Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314840 Share on other sites More sharing options...
searls03 Posted February 5, 2012 Author Share Posted February 5, 2012 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);} Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1314845 Share on other sites More sharing options...
searls03 Posted February 13, 2012 Author Share Posted February 13, 2012 how do I use this code....only instead of making columns, they make rows? Quote Link to comment https://forums.phpfreaks.com/topic/256395-query-results/#findComment-1317441 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.