0

I have a problem with the while loop. So i have this code that inserts feed and multiple files with a specific random code. For example i have this table named 'feed'

ID  | FEED      | FILE          | CODE
---------------------------------------
1   |Test post  |uploads/1.jpg  | 54231
2   |Test post  |uploads/2.jpg  | 54231
3   |Test post  |uploads/3.jpg  | 54231
4   |Test post  |uploads/4.jpg  | 54231

I need to print the feed only once if the code is repeated, in while loop. Here is my code:

<?php
  $connect = mysqli_connect("localhost","root","","test");

  // Check connection
  if (mysqli_connect_errno())
    {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }
  $q = mysqli_query($connect,"SELECT * FROM feed");
  $fetch = mysqli_fetch_array($q);
  while($fetch = mysqli_fetch_array($q)) {
  
 echo '<div class="post">'.$fetch['feed'].$fetch['file].'</div><br>';
 
  }
     ?> 
     

The above code outputs:

Test postuploads/1.jpg
Test postuploads/2.jpg
Test postuploads/3.jpg
Test postuploads/4.jpg

I need something that's like this:

Test post uploads/1.jpg / uploads/2.jpg/ uploads/3.jpg/ uploads/4.jpg

How to achieve this? and thanks in advance!

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Well, first of all you need to _not_ output an explicit linebreak if you want the output on a single line. – arkascha Sep 08 '18 at 06:38

3 Answers3

2

The easiest solution would be to change your query to use a GROUP BY and do a GROUP CONCAT on FILE:

$q = mysqli_query($connect,"SELECT FEED, GROUP_CONCAT(FILE) AS file, CODE FROM feed GROUP BY CODE");

This will give you one row per CODE value in your output, which will look something like:

FEED        FILE                                                         CODE
Test post   uploads/1.jpg, uploads/2.jpg, uploads/3.jpg, uploads/4.jpg   54231

If you want a separator other than , e.g. /, change the GROUP_CONCAT to

GROUP_CONCAT(FILE SEPARATOR '/')

Demo on Rextester

Update

There is also an error in the PHP code, you are setting $fetch = mysqli_fetch_array($q) before the while loop and then not doing anything with it, so you are losing one row of values. Try this code instead:

 $q = mysqli_query( $connect, "SELECT FEED AS feed, GROUP_CONCAT(FILE) AS file, CODE FROM feed GROUP BY CODE" );
 while ( $fetch = mysqli_fetch_array( $q ) ) {
    echo '<div class="post">' . $fetch[ 'feed' ] . $fetch[ 'file' ] . '</div><br>';
 }
Nick
  • 138,499
  • 22
  • 57
  • 95
1

Try this query this will be group file by code

 $q = mysqli_query($connect,"SELECT GROUP_CONCAT(file, ' ') AS files,* FROM feed GROUP BY code");
 $fetch = mysqli_fetch_array($q);
 while($fetch = mysqli_fetch_array($q)) {

 echo '<div class="post">'.$fetch['feed'].' '.$fetch['files'].'</div><br>';

 }
 ?> 

For more informations about Group_concat see here GROUP_CONCAT comma separator - MySQL

0
<?php
$connect = mysqli_connect("localhost","root","","test");


if (mysqli_connect_errno())
        {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
$q = mysqli_query($connect,"SELECT * FROM feed");                                                    
$fetch1 = mysqli_fetch_all($q);
$fetch2=$fetch1;


$fn1= count($fetch1);
$fn2=count($fetch2);

for($i=0;$i<$fn1;$i++)
    {        
        for($j=0;$j<$fn2;$j++)

        {
            if( $fetch1[$i][3] == $fetch2[$j][3])
                {
                    echo "duplicat";
                   echo "<div class='post'>";
                         print($fetch1[$i][1]);   print($fetch1[$i][2]); 
                    echo  "</div><br>";  

                    break 2;
                }
        }
    }



?>  
cool_benn
  • 56
  • 8