mySQL query fails in form code

ka katzenberg 26 Feb, 2010
hi there,

I would be very glad for help about a seemingly minor problem:
in my form I want to include a select list. The options from the list shall be the titles of the articles of a specific category. So I made up this code and pasted it in the "form code" area of the form:

<table class="newsletter" width="500" border="0" cellspacing="1" cellpadding="2">
  <tr> 
    <td>Seminar</td> 
	<td><select name="workshop">
		<?php 
		$db =& JFactory::getDBO();
		$query = "SELECT title FROM #__content WHERE catid = 6;";
		$db->setQuery($query);
		$result = $db->query();
		
		$titles = $db->loadResult();
		
		foreach ($titles as $title): ?>
				<option value="<?php echo ; ?>"><?php echo ; ?></option>
		<?php endforeach; ?>
      </select></td>
  </tr>
  <tr>
    <td> 
</td>
    <td><input name="absenden" type="submit" id="absenden" value="absenden"> </td>
  </tr>
</table>


but this returns a parse error: chronocontact.html.php(180) : eval()'d code on line 23

Help would be greatly appreciated. Thanks!
Gr GreyHead 26 Feb, 2010
Hi katzenberg,

There aren't 23 lines there so I guess you pasted an extract? Which is line 23 in the Form HTML box (could be a few lines either side of line 23 depending on text wrap)?

My guess is that the MySQL query isn't returning any values?? Have you out put $titles to check?

Bob
nm nml375 26 Feb, 2010
Hi Katzenberg,
This parse error would'nt happen to be "unexpected ';' in ..." ?
That would be caused by this piece of code:
<?php echo ; ?>

Simply put, you'll have to include something for echo to print, or it'll throw this error. Most likely, you intended to do something like this?
...
      foreach ($titles as $title): ?>
            <option value="<?php echo $title; ?>"><?php echo $title; ?></option>
      <?php endforeach; ?>
      </select></td>


/Fredrik
ka katzenberg 27 Feb, 2010
I kept on trying and at least the error is gone now.

Of course I had the $title after the echo because that is what I want to have in the select list - I only deleted it to test if the variable made problems.

But, you are right Greyhead, now the select field is empty. Any further hints?

Thanks again!
Gr GreyHead 27 Feb, 2010
Hi katzeberg.

Try the SQL query in PHPMyAdmin and see what result you get.

Bob
nm nml375 27 Feb, 2010
Hi katzeberg,
Had a deeper look at the code, and it seems you are using the loadResult() method?
This only returns the first field of the first row of the resultset. As such, it is not suitable for a foreach-loop.
You are probably looking for the loadAssocList() or loadObjectList() methods, which returns a list of arrays/objects (one array/object per row), which is suitable for use with foreach.

<table class="newsletter" width="500" border="0" cellspacing="1" cellpadding="2">
  <tr>
    <td>Seminar</td>
   <td><select name="workshop">
      <?php
      $db =& JFactory::getDBO();
      $query = "SELECT title FROM #__content WHERE catid = 6;";
      $db->setQuery($query);
      
      $result = $db->loadObjectList();
      
      foreach ($result as $item): ?>
            <option value="<?php echo $item->title; ?>"><?php echo $item->title; ?></option>
      <?php endforeach; ?>
      </select></td>
  </tr>
  <tr>
    <td>
</td>
    <td><input name="absenden" type="submit" id="absenden" value="absenden"> </td>
  </tr>
</table>


/Fredrik
ka katzenberg 01 Mar, 2010
I tried to keep it simple but maybe it was too simple. Thanks to Frederic it works now with the loadObjets

THANKS

Daniel
This topic is locked and no more replies can be posted.