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