Jump to content

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

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.