Jump to content

Need help with Outputting Correctly


gaza165

Recommended Posts

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

 

Link to comment
https://forums.phpfreaks.com/topic/191547-need-help-with-outputting-correctly/
Share on other sites

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

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.

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

 

 

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

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

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.