php my sql echo result only once but show all else in row

Discussion in 'PHP' started by saturn100, Aug 30, 2014.

  1. #1
    OK
    I hope someone can help me with this

    I have a table with 4 columns
    lets say issue 1 issue 2 issue 3 issue 4
    None of them are unique

    I want to echo out the rows in a table
    BUT I dont want to repeat the text in issue one
    so lets day I have an entry in issue one with the value of Climate change
    I only want to echo out climate change once but want to echo out all the other fields if and when climate change in repeated

    I hope that made some sense
    I tried distinct and group by but they will leave out all of the row where issue 1 is repeated I only want to leave out the field issue 1

    I have some code written but It is only echoing the field issue1 once and leaving the rest black
    I think the problem is with the if else statement the else alternative isnt showing at all


    my code as it stands is
    
    <?php
    $con=mysqli_connect("localhost","","password","");
    // Check connection
    if (mysqli_connect_errno()) {
      echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
    
    $resultx = mysqli_query($con,"SELECT distinct issue  FROM table1  ; ");
    
    $result = mysqli_query($con,"SELECT  *  FROM table1  ; ");
    
    
    
    echo "<table border='1'>
    <tr>
    <th>header 1</th>
    <th>header 2</th>
    <th>Header 3</th>
    <th>Header 4</th>
    
    </tr>";
    while($row = mysqli_fetch_array($result)) {
    while($rowx = mysqli_fetch_array($resultx)) {
    
    $issues = $rowx['issue1'];
    echo $issues;
    
      echo "<tr>";
     
    if ($issues == $row['issue1']){
      echo "<td colspan ='5'> " . $row['issue1'] . "</td>";
      }
      else {"<td colspan ='5'> blank </td>";}
    
    }
    
    
      echo "</tr><tr>";
    
    
    
    
    
    
      echo "<td>" . $row['issue2'] . "</td>";
      echo "<td>" . $row['issue3'] . "</td>";
      echo "<td>" . $row['issue4'] . "</td>";
    
      echo "</tr>";
    }
    
    
    echo "</table>";
    
    mysqli_close($con);
    ?>
    
    Code (markup):
    at the moment its only printing out the first issue1 row and nothing for the rest
    Thanks
     
    saturn100, Aug 30, 2014 IP
  2. HostPlanz

    HostPlanz Well-Known Member

    Messages:
    449
    Likes Received:
    34
    Best Answers:
    4
    Trophy Points:
    130
    #2
    It's hard to understand what you've explained
     
    HostPlanz, Aug 30, 2014 IP
  3. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #3
    this thread on a different site is on about the same thing
    But I cant follow the answers

    http://forums.phpfreaks.com/topic/275001-while-loop-remove-duplicates-and-echo-once/
     
    saturn100, Aug 30, 2014 IP
  4. saturn100

    saturn100 Well-Known Member

    Messages:
    465
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    111
    #4
    Ok finally got something I found online working
    Would still love to know where I was going wrong however
     
    saturn100, Aug 30, 2014 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    28,897
    Likes Received:
    4,555
    Best Answers:
    123
    Trophy Points:
    665
    #5
    That was a really ugly way of tackling the problem

    I'd rather you query once and order by 'Issue', then date or whatever so that all the similar Issues are grouped together

    then as you loop through the results you have a variable called $prevIssue.
    $prevIssue starts with a value of false

    when you go to echo out the current issue value, check to see if $row['Issue'] == $prevIssue. If it is then echo out '&nbsp;', else echo out the Issue text.

    You will find you may want to do the same for submit dates or assigned to depending on the order of the table at different times.
     
    sarahk, Aug 30, 2014 IP