line after duplicate values

Discussion in 'PHP' started by mero2020, Apr 10, 2014.

  1. #1
    $sql = "SELECT * FROM issue ,books where issues.contact_id= books.contact_id";



    notitleyear

    214book12014

    214book12014

    599book32014

    599book32014

    599book32014
    964book22014

    964book22014



    I want to add line after same values to be like this

    214
    214
    ----------------------------------------------
    599
    599
    599
    -------------------------------------------

    I tried to added them to array but can't did it??????
     
    mero2020, Apr 10, 2014 IP
  2. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #2
    Show us your code.
     
    ThePHPMaster, Apr 10, 2014 IP
  3. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #3
    You have to loop through the result set somehow, to output it - just store the value, and if the stored value is the same, output a <hr> or something similar.
    mock code:
    
    // assumes  you're outputting via a while
    $formervalue = '';
    $c = 0;
    while ($row = $stmt->fetch()) {
      $db_result = $row['no'].' - '.$row['title'].' - '.$row['year'];
      if (!empty($formervalue) && $formervalue == $outputvalue) {
      $c++;
      }
      echo $outputvalue = (!empty($formervalue) && $c == 0) ? '<hr>'.$db_result : $db_result;
      $formervalue = $outputvalue;
    }
    
    PHP:
    That should do what you want, more or less
     
    PoPSiCLe, Apr 12, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    I'd probably simplify that down even further, getting rid of the counter and leveraging $formerValue as a boolean false to detect the first item... axing 'outputvalue' in the process and not repeating myself on the value echo. I'd probably also condense formerValue's assignment into the echo thus:

    $lastValue = false;
    while ($row = $stmt->fetch()) {
    	$output = $row['no'] . ' - ' . $row['title'] . ' - ' . $row['year'];
    	if ($lastValue !== false) echo $lastValue == $output ? '<br />' : '<hr />';
    	echo $lastValue = $output;
    }
    Code (markup):
    Though I suspect his output is from a single element in that $row, in which case it would probably go something like this:

    $lastValue = false;
    while ($row = $stmt->fetch()) {
    	if ($lastValue !== false) echo $lastValue == $row['notitleyear'] ? '<br />' : '<hr />';
    	echo $lastValue = $row['notitleyear'];
    }
    Code (markup):
    In either case, gut out all the extra 'variables for nothing'. Of course, this is all on the assumption that the database only returns string values and/or you want it to output any 'empty' results if present. If you also want empty results to not have breaks or rules...

    $lastValue = '';
    while ($row = $stmt->fetch()) {
    	if (!empty($lastValue)) echo $lastValue == $row['notitleyear'] ? '<br />' : '<hr />';
    	echo $lastValue = $row['notitleyear'];
    }
    Code (markup):
    Could be used.
     
    deathshadow, Apr 12, 2014 IP