bluefrog Posted June 12, 2016 Share Posted June 12, 2016 Hi all, thank you for taking time to browse my thread. I have had some code written for me a while back which fetches either 1 or 2 rows from a database depending on if there are one or two rows based on the date. What it does is simply keep track of lotto results for each of the 2 daily draws along with the booster ball. Now, here's what I'm trying to do. Each row should really have it's own header such as Lunchtime or Teatime based on the draw number fetched from the database. I'm struggling a little. I understand how the code works and that I should have some kind of if/else (I think) in there, but I can't understand where or how to place it. Here is the current code <?php $color_mapping=array('0'=>'BLUE','1'=>'GREEN','2'=>'RED','3'=>'ORANGE','4'=>'YELLOW','5'=>'BROWN','6'=>'VIOLET', '7'=>'BLUE'); for($i = 0; $i < count($rows); $i++) { $res = explode(",", $rows[$i]['result']); $rRows = array(); //echo "<tr><td><p style='float:left;'>".$rows[$i]["draw_nr"].".</p></td>"; for($j = 0; $j < count($res); $j++) echo "<img class='ball' src='http://www.number49s.co.uk/images/fortyninesballs/".$res[$j].".gif'>"; echo "<td><img class='ball booster img-responsive' src='http://www.number49s.co.uk/images/fortyninesballs/".$rows[$i]["booster_ball"].".gif'></td><p style='clear:both;'></p>"; } From what I understand it fetches the results, turns them into an array and pulls each ball separately, from the rows and then appends the booster ball result for the current row. I'm thinking that it should have an if statement around $rRows array which reads if there is only a single row (based on draw number and date) and if it is outputs the code for the single row, then the else statement outputs the code for both. With a different header for each row I know I look like an idiot to you guys (and gals) who know this stuff inside out, but I am trying to learn although sadly this is WAY out of my ability at the moment. Any help as to where to place the if statement and how to get the results based on row number would be very gratefully accepted, but please don't be rude, I know I'm asking a lot, and I also know I know very little compared to the rest of you, but it doesn't give you warrant to be nasty. Thank you in advance to anybody who may be able to help me get this working, it's been driving me nuts for over 3 hours now Quote Link to comment https://forums.phpfreaks.com/topic/301330-different-headers-for-each-row-from-database/ Share on other sites More sharing options...
ginerjm Posted June 12, 2016 Share Posted June 12, 2016 Your description of the process doesn't make sense at all. I would think that the results row would contain all of the balls and would already be in an array and wouldn't need any manipulation. How about showing us the query statement? Quote Link to comment https://forums.phpfreaks.com/topic/301330-different-headers-for-each-row-from-database/#findComment-1533592 Share on other sites More sharing options...
bluefrog Posted June 12, 2016 Author Share Posted June 12, 2016 (edited) Thank you for the reply Each ball is stored individually, I'm not really sure past that, I don;t work with databases at all normally. Here is the entire code: $select = mysqli_query($connection, "SELECT * FROM lotto where date = (select max(date) from lotto) ORDER BY draw_nr"); $rows = array(); while($row = $select->fetch_array()) $rows[] = $row; ?> <style> table { border-collapse: collapse; text-align: center; } </style> <h1><center>Results for <?php echo isset($rows[0]) ? date('l jS F', strtotime($rows[0]['date'])) : ""; ?> 49s Lotto</center></h1> <p>Draws are updated around 1pm and 6pm GMT. As requested by so many of you, the draw will now be shown in the order the balls were drawn. If you want the balls in numerical order, look to the right, or if using a mobile device, scroll right to the bottom.</p> <p> </p> <div class="draw-container"> <div> <div class="balls-heading"> <strong>Results</strong> </div> <div class="booster-heading"> <strong>Booster</strong> </div> </div> <div class="clr"></div> <div> <?php $color_mapping=array('0'=>'BLUE','1'=>'GREEN','2'=>'RED','3'=>'ORANGE','4'=>'YELLOW','5'=>'BROWN','6'=>'VIOLET', '7'=>'BLUE'); for($i = 0; $i < count($rows); $i++) { $res = explode(",", $rows[$i]['result']); $rRows = array(); echo "<p style='float:left;' class='drawnumber'>(Draw number ".$rows[$i]["draw_nr"].")</p><p> </p>"; for($j = 0; $j < count($res); $j++) echo "<img class='ball' src='http://www.mysite.co.uk/images/fortyninesballs/".$res[$j].".gif'>"; echo "<td><img class='ball booster img-responsive' src='http://www.mysite.co.uk/images/fortyninesballs/".$rows[$i]["booster_ball"].".gif'></td><p style='clear:both;'></p>"; } ?> </div> </div> Edited June 12, 2016 by bluefrog Quote Link to comment https://forums.phpfreaks.com/topic/301330-different-headers-for-each-row-from-database/#findComment-1533593 Share on other sites More sharing options...
bluefrog Posted June 12, 2016 Author Share Posted June 12, 2016 Was just wondering actually, would it be easier to replace the draw number? The code above will place draw number 1 for example, but all the code really does is output 1 or 2, is it possible to replace the number 1 or 2 with a string? Quote Link to comment https://forums.phpfreaks.com/topic/301330-different-headers-for-each-row-from-database/#findComment-1533594 Share on other sites More sharing options...
ginerjm Posted June 12, 2016 Share Posted June 12, 2016 Can you tell me the field names that are being retrieved or at least how many are being returned in each row? Is the drawing date/time included in each row? What else? General practice is to avoid using * in selects. Quote Link to comment https://forums.phpfreaks.com/topic/301330-different-headers-for-each-row-from-database/#findComment-1533596 Share on other sites More sharing options...
ginerjm Posted June 12, 2016 Share Posted June 12, 2016 Hmmmmm It appears that the db is not normalized and that the numbers ARE all in one row, probably with a date/time field. You are grabbing each row into an array (silly) and then exploding the result value into an array by comma. That tells me that your db looks like: 06/09/16 41,22,33,44,55....... and you are making it an array that you then loop thru and output some img tags to be viewed. You should also try and separate the html code form the php code to make it easier to understand and maintain. You nave all those div tags but you never put anything in them! Quote Link to comment https://forums.phpfreaks.com/topic/301330-different-headers-for-each-row-from-database/#findComment-1533597 Share on other sites More sharing options...
ginerjm Posted June 12, 2016 Share Posted June 12, 2016 (edited) I don't know what you are trying to make this look like, but the pure logic below will give you something like: Draw number: nnnnnn Balls: 1, 2, 3, 4, 5, 6 $q = "SELECT * FROM lotto where date = (select max(date) from lotto) ORDER BY draw_nr"); $select = mysqli_query($connection, $q); $output = ''; while($row = $select->fetch_array()) { $output .= "Draw number {$row['draw_nr']}<br>"; $nums = explode(',',$row['result']); sort($nums); // put nums in order $numstr = implode(', ',$nums); $output .= "Balls: $numstr<br>"; } You have the img tags and all the color stuff which makes no sense to me but I've boiled all your code down to these few lines. You can write a SEPERATE block of html code at the bottom of your script and place the $output variable in it where it belongs if you want to make it pretty. PS - this will do however many drawings exist with the latest date on them, which might be a good thing to add to your output also. Edited June 12, 2016 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/301330-different-headers-for-each-row-from-database/#findComment-1533598 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.