Good day! Unable to display data, error:

Parse error: syntax error, unexpected 'foreach'(T_FOREACH) 

Error 99 line. To be precise, here:

 . foreach($items as $r2 => $value) { "".iconv("windows-1251", "utf-8",$r2['TITLE'])."" 

Help to understand. All code:

 <?php //подключение к серверу БД class db { public function __construct() { global $dbh; if (!is_null($dbh)) return; $dbh = mssql_Pconnect('server', 'SMViewer', 'password') or die('Не могу подключится к Базе данных'); mssql_select_db('SM7',$dbh) or die('Не могу выбрать базу данных'); } public function select_list($query) { $q = mssql_query($query); if (!$q) return null; $ret = array(); while ($row [] = mssql_fetch_array($q, MSSQL_ASSOC)) { array_push($ret, $row); } mssql_free_result($q); return $ret; } } $db = new db(); //Select the contacts SM (СУЭ) you want to show in your PDF file $query = 'SELECT a1.LOCATION_NAME, a1.LOCATION_CODE, a1.CITY, a1.EMAIL, c1.DEPT_NAME, c1.LOCATION, c1.EMAIL AS C_MAIL, c1.ACTIVE, c1.FULL_NAME, c1.TITLE, c1.CONTACT_PHONE, c1.EXTENSION, c1.ROOM FROM CONTCTSM1 c1 INNER JOIN DEPTM1 b1 ON b1.DEPT_NAME=c1.DEPT_NAME INNER JOIN LOCM1 a1 ON a1.LOCATION_CODE=c1.LOCATION'; $row = $db->select_list($query); if(count($row)) { $lorem = ''; foreach($row as $r => $items) { $loremed .= '<h4>' . "".iconv("windows-1251", "utf-8",$r['DEPT_NAME'])."" . '</h4><table class="bpmTopicC"> <thead> <tr class="headerrow"> <th>Должность</th> <td> <p>ФИО</p> </td> <td> Номер ВТС </td> <td> Городской номер </td> <td> Номер кабинета </td> </tr> </thead> <tfoot> <tr class="footerrow"> <th>Должность</th> <td> <p>ФИО</p> </td> <td> Номер ВТС </td> <td> Городской номер </td> <td> Номер кабинета </td> </tr> </tfoot> <tbody> <tr> <th>' . foreach($items as $r2 => $value) { "".iconv("windows-1251", "utf-8",$r['TITLE'])."" . '</th> <td>' . "".iconv("windows-1251", "utf-8",$r['FULL_NAME'])."" . '</td> <td>' . "".iconv("windows-1251", "utf-8",$r['EXTENSION'])."" . '</td> <td>' . "".iconv("windows-1251", "utf-8",$r['CONTACT_PHONE'])."" .'</td> <td>' . "".iconv("windows-1251", "utf-8",$r['ROOM'])."" . '</td>' . } . '</tr> </tbody> </table>'; $lorem = $loremed; } } else {} //============================================================== //============================================================== //============================================================== //$lorem2 = ''; $html = ' <!-- defines the headers/footers --> <!--mpdf <htmlpageheader name="myHTMLHeader"> <div style="text-align: right; border-bottom: 1px solid #000000; font-family: serif; font-size: 8pt;">Зюзик</div> </htmlpageheader> <htmlpageheader name="myHTMLHeaderEven"> <div style="text-align: left; border-bottom: 1px solid #000000; font-family: serif; font-size: 8pt;">Even Header</div> </htmlpageheader> <htmlpagefooter name="myHTMLFooter"> <table width="100%" style="border-top: 1px solid #000000; vertical-align: top; font-family: sans; font-size: 8pt;"><tr> <td width="33%">{DATE Ymd}</td> <td width="33%" align="center"><span style="font-size:12pt;">{PAGENO}</span></td> <td width="33%" style="text-align: right;">Телефонный справочник</td> </tr></table> </htmlpagefooter> <htmlpagefooter name="myHTMLFooterEven"> <table width="100%" style="border-top: 1px solid #000000; vertical-align: top; font-family: sans; font-size: 8pt;"><tr> <td width="33%">Even Footer</td> <td width="33%" align="center"><span style="font-size:12pt;">{PAGENO}</span></td> <td width="33%" style="text-align: right;">{DATE Ymd}</td> </tr></table> </htmlpagefooter> <htmlpageheader name="tocHTMLHeader"> <div style="text-align: right; border-bottom: 1px solid #000000; font-family: serif; font-size: 8pt;">Зюзик</div> </htmlpageheader> <htmlpageheader name="tocHTMLHeaderEven"> <div style="text-align: left; border-bottom: 1px solid #000000; font-family: serif; font-size: 8pt;">ToC Even Header</div> </htmlpageheader> <htmlpagefooter name="tocHTMLFooter"> <table width="100%" style="border-top: 1px solid #000000; vertical-align: top; font-family: sans; font-size: 8pt;"><tr> <td width="33%">{DATE Ymd}</td> <td width="33%" align="center"><span style="font-size:12pt;">{PAGENO}</span></td> <td width="33%" style="text-align: right;">Телефонный справочник</td> </tr></table> </htmlpagefooter> <htmlpagefooter name="tocHTMLFooterEven"> <table width="100%" style="border-top: 1px solid #000000; vertical-align: top; font-family: sans; font-size: 8pt;"><tr> <td width="33%">ToC Even Footer</td> <td width="33%" align="center"><span style="font-size:12pt;">{PAGENO}</span></td> <td width="33%" style="text-align: right;">{DATE Ymd}</td> </tr></table> </htmlpagefooter> mpdf--> <h1>Зюзик</h1> <h2>Телефонный справочник</h2> <!-- set the headers/footers - they will occur from here on in the document --> <tocpagebreak paging="on" links="on" toc-odd-header-name="html_tocHTMLHeader" toc-even-header-name="html_tocHTMLHeaderEven" toc-odd-footer-name="html_tocHTMLFooter" toc-even-footer-name="html_tocHTMLFooterEven" toc-odd-header-value="on" toc-even-header-value="on" toc-odd-footer-value="on" toc-even-footer-value="on" toc-preHTML="&lt;h2&gt;СОДЕРЖАНИЕ&lt;/h2&gt;" toc-bookmarkText="Content list" resetpagenum="1" pagenumstyle="" odd-header-name="html_myHTMLHeader" odd-header-value="on" even-header-name="html_myHTMLHeaderEven" even-header-value="ON" odd-footer-name="html_myHTMLFooter" odd-footer-value="on" even-footer-name="html_myHTMLFooterEven" even-footer-value="on" outdent="2em" /> '; //============================================================== include("mpdf.php"); $mpdf=new mPDF('c','A4','','',32,25,27,25,16,13); $mpdf->mirrorMargins = 1; $mpdf->SetDisplayMode('fullpage','two');$mpdf = new mPDF('utf-8'); $mpdf->ignore_invalid_utf8 = false;$mpdf->SetAutoFont(AUTOFONT_ALL); // LOAD a stylesheet $stylesheet = file_get_contents('mpdfstyleA4.css'); $mpdf->WriteHTML($stylesheet,1); // The parameter 1 tells that this is css/style only and no body/html/text $mpdf->WriteHTML($html); // Alternative ways to mark ToC entries and Bookmarks // This will automatically generate entries from the <h4> tag $mpdf->h2toc = array('H4'=>0); $mpdf->h2bookmarks = array('H4'=>0); //============================================================== // CONTENT for ($j = 1; $j<2; $j++) { //if ($j==2) $mpdf->WriteHTML('<pagebreak resetpagenum="0" pagenumstyle="a" />',2); //if ($j==3) $mpdf->WriteHTML('<pagebreak resetpagenum="1" pagenumstyle="I" />',2); //if ($j==4) $mpdf->WriteHTML('<pagebreak resetpagenum="0" pagenumstyle="i" />',1); //if ($j==1) $mpdf->WriteHTML('<pagebreak resetpagenum="0" pagenumstyle="1" />',1); //if ($j==6) $mpdf->WriteHTML('<pagebreak resetpagenum="1" pagenumstyle="1" type="NEXT-ODD" /><div style="color:#AA0000">ODD</div>',2); for ($x = 1; $x<2; $x++) { // Alternative way to mark ToC entries and Bookmarks manually //$mpdf->WriteHTML('<h4>Section '.$j.'.'.'<bookmark content="Section '.$j.'.'.$x.'" level="0" /><tocentry content="Section '.$j.'.'.$x.'" level="0" /></h4>',2); // Using Automatic generation from <h4> tag //$mpdf->WriteHTML('<h4>'.$lorem2.'</h4>',2); $html = ''; // Split $lorem into words $words = preg_split('/([\s,\.]+)/',$lorem,-1,PREG_SPLIT_DELIM_CAPTURE); foreach($words as $i => $e) { if($i%2==0) { $y = rand(1,10); // every tenth word if (preg_match('/^[а-яА-Я]{4,99}$/',$e) && ($y > 8)) { // If it is just a word use it as an index entry $content = ucfirst(trim($e)); $html .= '<indexentry content="'.$content.'" />'; $html .= '<i>'.$e . '</i>'; } else { $html .= $e; } } else { $html .= $e; } } $mpdf->WriteHTML($html); } } //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // INDEX $mpdf->Output(); exit; //============================================================== //============================================================== ?> 

Run print_r ($ row); :

 Array ( [0] => Array ( [DEPT_NAME] => Отдел №1 [LOCATION_NAME] => 19 регион [LOCATION_CODE] => 19 [LOCATION] => 19 [C_MAIL] => test@mail.ru [ACTIVE] => f [FULL_NAME] => Иванов Иван Иванович [TITLE] => профессор [CONTACT_PHONE] => (391)2-22-22-22 [EXTENSION] => 0186 0001 [ROOM] => 6-05 ) [1] => Array ( [DEPT_NAME] => Отдел №1 [LOCATION_NAME] => 19 регион [LOCATION_CODE] => 19 [LOCATION] => 19 [C_MAIL] => test@mail.ru [ACTIVE] => f [FULL_NAME] => Иванов Иван Иванович [TITLE] => профессор [CONTACT_PHONE] => (391)2-22-22-22 [EXTENSION] => 0186 0001 [ROOM] => 6-05 ) [2] => Array ( [DEPT_NAME] => Отдел №3 [LOCATION_NAME] => 19 регион [LOCATION_CODE] => 19 [LOCATION] => 19 [C_MAIL] => test@mail.ru [ACTIVE] => f [FULL_NAME] => Иванов Иван Иванович [TITLE] => профессор [CONTACT_PHONE] => (391)2-22-22-22 [EXTENSION] => 0186 0001 [ROOM] => 6-05) .... 

I need to bring to mind:

 > Наименование подразделения №1 > > ФИО Номер телефона ВТС номер комнаты > .... > ФИО Номер телефона ВТС номер комнаты > Наименование подразделения №2 > > ФИО Номер телефона ВТС номер комнаты > .... > ФИО Номер телефона ВТС номер комнаты 

Closed due to the fact that off-topic participants Dmitriy Simushev , aleksandr barakin , Alex , Bald , Denis 14 Dec '16 at 6:47 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Dmitriy Simushev, aleksandr barakin, Alex, Bald, Denis
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Construction . foreach // ... (note the dot) is syntactically incorrect. The question is about nothing. - Dmitriy Simushev

1 answer 1

I don't even know why you think this should work. Change the fragment from foreach($items as $r2 => $value) to this:

 <tr><th>'; foreach($items as $r2 => $value) { $loremed .= iconv("windows-1251", "utf-8",$r['TITLE']) . '</th><td>' . iconv("windows-1251", "utf-8",$r['FULL_NAME']) . '</td><td>' . iconv("windows-1251", "utf-8",$r['EXTENSION']) . '</td><td>' . iconv("windows-1251", "utf-8",$r['CONTACT_PHONE']) .'</td><td>' . iconv("windows-1251", "utf-8",$r['ROOM']) . '</td>'; } $loremed .= '</tr> 
  • The error has gone. Thank you, but now the result is a white screen. Do not tell me what could be the problem. I have a suspicion that I am not using the correctly derived $ row array in double foreach. I need to display information from the database: First, the name of the department, then all the staff of this department. And so with each unit. All this needs to be displayed on one page. - intertex
  • @intertex, it is likely that you are incorrectly processing the $ row array, but I do not know what is contained in this array, so at this stage I can hardly help with anything. - Pyramidhead
  • Look please, I laid out the resulting array. - intertex
  • one
    @intertex, in the foreach($row as $r => $items) , the $r variable contains only an identifier, but $items contains an associative array. Thus, instead of $r['DEPT_NAME'] and the like, $items['DEPT_NAME'] should be used. The second foreach($items as $r2 => $value) does not make sense at all. Just also output the necessary values ​​without any loop: $items['TITLE'] , etc. Then you will understand it yourself. - Pyramidhead
  • Look, I added to the description, what kind of I need to lead. - intertex