gaza165 Posted February 9, 2010 Share Posted February 9, 2010 The code below I have does exactly what i want to do.. B Badminton Basketball Bowles C Cricket <div class="sport-wrapper"> <?php include("./dbconnect/dbconnect.php"); $sql = mysql_query("SELECT sport_name,sport_image FROM all_sports ORDER BY sport_name"); $prev = ''; while ($row = mysql_fetch_row($sql) ) { echo "<div class='sport'>"; $game = $row[0]; $initial = $game{0}; if ($prev != $initial) { echo "<h1>$initial</h1>"; $prev = $initial; } echo "<p>".$game."</p>"; echo "</div>"; } ?> </div> However i want it to come out like so <div class="sport"> <h1>B</h1> <p>Badminton</p> <p>Basketball</p> </div> <div class="sport"> <h1>C</h1> <p>Cricket</p> </div> However this is the way it comes out <div class='sport'><h1>B</h1><p>Badminton</p></div> <div class='sport'><p>Basketball</p></div> Even though basketball is in the B Basically i want a div to wrap around all B's... all C's etc... Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/ Share on other sites More sharing options...
Ty44ler Posted February 9, 2010 Share Posted February 9, 2010 \n where you want to break the line in the code Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009727 Share on other sites More sharing options...
gaza165 Posted February 9, 2010 Author Share Posted February 9, 2010 ?????? dont understand ???? I dont want to add a break i want to wrap all the B's...then wrap all the C's then wrap all the D's etc Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009728 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 <div class="sport-wrapper"> <?php include("./dbconnect/dbconnect.php"); $sql = mysql_query("SELECT sport_name,sport_image FROM all_sports ORDER BY sport_name"); $prev = ''; echo "<div class='sport'>"; while ($row = mysql_fetch_row($sql) ) { $game = $row[0]; $initial = $game{0}; if ($prev != $initial) { echo "</div><div class='sport'>"; echo "<h1>$initial</h1>"; $prev = $initial; } echo "<p>".$game."</p>"; } echo "</div>"; ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009729 Share on other sites More sharing options...
Ty44ler Posted February 9, 2010 Share Posted February 9, 2010 So... echo "<div class='sport'>"; would become this: echo "<div class='sport'>\n"; \n will insert a line break within your code so when you view source it will be neater. If you want to actually insert a line break into your output you could echo a <br />. EDIT: Sorry, I see what you're saying now. I got confused when you typed the code differently the last time. Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009730 Share on other sites More sharing options...
gaza165 Posted February 9, 2010 Author Share Posted February 9, 2010 I dont think you understand what i want Using the code ive got it outputs like this.... <div class='sport'> <h1>B</h1> <p>Badminton</p> </div> <div class='sport'> <p>Basketball</p> </div> This is wrong, i want it to output like this <div class="sport"> <h1>B</h1> <p>Badminton</p> <p>Basketball</p> </div> <div class="sport"> <h1>C</h1> <p>Cricket</p> </div> You can see that basketball is in the same div here. This is what i want.. Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009733 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 Yes, my version of your code should do just that it starts with a div when it finds a new initial it ends the div and starts another and adds a /div on the end to complete the picture This way you will get divs for the same conditions that you write the h1 for Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009735 Share on other sites More sharing options...
gaza165 Posted February 9, 2010 Author Share Posted February 9, 2010 JL5501 your code works perfectly however it puts a blank <div class='sport'></div> is there any reason why that happens?? Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009738 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 yes it will I see. You need to check if $initial is being set for the first time, and if yes, not output the div echo Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009745 Share on other sites More sharing options...
jl5501 Posted February 9, 2010 Share Posted February 9, 2010 something like <div class="sport-wrapper"> <?php include("./dbconnect/dbconnect.php"); $sql = mysql_query("SELECT sport_name,sport_image FROM all_sports ORDER BY sport_name"); $prev = ''; echo "<div class='sport'>"; while ($row = mysql_fetch_row($sql) ) { $game = $row[0]; $initial = $game{0}; if ($prev != $initial) { if($prev) echo "</div><div class='sport'>"; echo "<h1>$initial</h1>"; $prev = $initial; } echo "<p>".$game."</p>"; } echo "</div>"; ?> </div> as you have your $prev initialised to '' then that should do it Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009746 Share on other sites More sharing options...
gaza165 Posted February 9, 2010 Author Share Posted February 9, 2010 Thanks very much guys, you have been extremely helpful!! Quote Link to comment https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/#findComment-1009749 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.