I have 2 separate foreach loops being executed in the following code. At the end I have $row->websiteURL outputting right after the second foreach loop. The problem is $row->websiteURL is part of the first loop. So I get the following error when I run the code:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: stdClass::$websiteURL
Filename: views/projects.php
Line Number: 135
” target=”_blank”>
How would I go about continuing the first foreach loop after the second foreach is complete?
<table style="width: 41%">
<?php $query = $this->db->query("SELECT * FROM projects ORDER BY idprojects DESC");
foreach ($query->result() as $row) { ?>
<tr>
<td style="height: 15px">
<div class="auto-style2">
<em><span class="auto-style1"><?=$row->projectName?></span></div>
<table cellpadding="5" style="width: 535px; height: 66;">
<tr>
<td class="box1" style="height: 49px; width: 800px;">
<table cellpadding="0" cellspacing="0" style="width: 466px; height: 185px">
<!-- MSTableType="layout" -->
<tr>
<td valign="top" style="width: 225px">
<em>
<table style="width: 100%">
<tr class="box1">
<td class="innerbox" style="height: 88px"><em><span class="text1">Project name</span>:
<span class="underlined-link"><?=$row->projectName?></span><br>
<span class="text1">Description</span>: <?=$row->projectDesc?><br><span class="text1">Start
date</span>: <?=$row->startDate?><br><span class="text1">Finised date</span>:
<?=$row->finishedDate?><br><span class="text1">Created for</span>:
<?=$row->createdFor?><br><span class="text1">Contributers</span>:
<span class="underlined-link"><?=$row->contributors?></span></em></td>
</tr>
</table>
</em></td>
<td style="width: 12px"> </td>
<td valign="top" style="height: 185px; width: 229px">
<em>
<?php $query = $this->db->query("SELECT * FROM screenshots ORDER BY idscreenshot DESC");
foreach ($query->result() as $row) { ?>
<img alt="" src="<?=$row->screenshotURI?>" width="231" height="187"></em> </td>
</tr>
<?php } ?>
</table>
<br>
<a style="text-decoration:none" href="<?=$row->websiteURL?>" target="_blank"><div class="link1">
View This Product</div></a>
</td>
</tr>
</table>
</em></td>
</tr>
<?php } ?>
</table>
You need to change your variables in the second foreach like this:
The $row variable is still in scope and it’s looking in the first query for this. If you are doing an inner foreach loop you can’t name the “item” variable the same as the outter foreach.
so more to the point:
Back to the bigger issue. You should not being querying data in the views. Data is manipulated in the Model. The controller loads the model, uses the model to get the information, then passes the info to the view. It can be a difficult concept to grasp if you’re new to MVC. Once you get this working, focus on moving these queries into their own functions in your model and retrieving the information that way.