Trouble Displaying an array of artists from a database.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Trouble Displaying an array of artists from a database.

Post by mmc01ms »

I have a form where a user adds new data about a vinyl to a database. When the come to selecting the artist i want an option field which displays all the artists in the database so they can select the one they want. The trouble im having is that i have created the following functions:

Code: Select all

<?php require_once('common_db.php');
	
	
		
	function db_result_to_array($result)
	{
		$res_array = array();
		
		for ($count=0; $row = @mysql_fetch_array($result); $count++)
			$res_array[$count] = $row;
		
		return $res_array;
	}
	
	function get_artists()
	{
		$link_id = db_connect();
		$query = 'select artist_id from artists';
		
		$result = @mysql_query($query);
		
		if (!result)
			return false;
		$num_artists = @mysql_num_rows($result);
		if($num_artists == 0)
			return false;
		
		$result = db_result_to_array($result);
		return $result;
	}
	
	
	
	
	function addrecord(){
?>

<td width="581" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr> 
			<td valign="top" width="581" height="600"><form name="newrecord" method="post" action="addnewrecord.php?">
					<table>
						<font color="#000000" size="2" face="Courier New, Courier, mono">
							<tr>
								<td>Title: </td>
								<td><input name="title" type="text" maxlength="40" id="title"></td>
							</tr>
							<tr>
								<td>Artist: </td>
								<td><select name="artist_id">
										<?php
		$artist_array = get_artists();
		foreach ($artist_array as $thisartist)
		{
			echo '<option value="';
			echo $thisartist['artist_id'];
			echo '"';
			echo '>';
			echo"\n";
		}
										?>
									</select>
								</td></table></form>	
						
						<div align="left"></div></td>
				</tr>
			</table></td>
	</tr>
</table>
<?
	}?>
however all i get is a title text box and an artist option field with no data in? Any ideas guys?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Trouble Displaying an array of artists from a database.

Post by timvw »

af first sight there is nothing wrong with your php code, but i see a problem with your html code

Code: Select all

&lt;select&gt;
&lt;option value='1'&gt;
&lt;option value='2'&gt;
&lt;/Select&gt;
this is what your code generates, save it in a file as .html and view it in a browser.

you probably want:

Code: Select all

&lt;option value='1'&gt;1&lt;/option&gt;


mmc01ms wrote:

Code: Select all

function db_result_to_array($result)
	{
		$res_array = array();
		
		for ($count=0; $row = @mysql_fetch_array($result); $count++)
			$res_array[$count] = $row;
		
		return $res_array;
	}
you can rewrite the loop as, no need for keeping a counter ;)

Code: Select all

while ($row = mysql_fetch_array($result)
   $res_array[] = $row;
mmc01ms wrote:

Code: Select all

function get_artists()
	{
		$link_id = db_connect();
		$query = 'select artist_id from artists';
		
		$result = @mysql_query($query);
so you have a link to your db, $link_id.. I don't see why you don't use it ...

Code: Select all

$result = @mysql_query($query, $link_id);
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

thanks for the quick feedback i've tried your soloutions and it made sense however i still don't get any output from the query. I actually looked in the html output and it produced an error which said:

Code: Select all

&lt;b&gt;Warning&lt;/b&gt;:  Invalid argument supplied for foreach() in &lt;b&gt;c:\program files\apache group\apache2\htdocs\useful_stock_fns.php&lt;/b&gt; on line &lt;b&gt;54&lt;/b&gt;&lt;br /&gt;
the code for that is:

Code: Select all

<?php 
function db_result_to_array($result)    
	{
		$res_array = array();
		for ($count=0; $row = @mysql_fetch_array($result); $count++)  
			$res_array[$count] = $row;                
		return $res_array;   
	}
	
	
	function get_artists()
	{
		$link_id = db_connect();
		$query = 'select artist_id from artists';
		
		$result = @mysql_query($query, $link_id);
		
		if (!result)
			return false;
		$num_artists = @mysql_num_rows($result);
		if($num_artists == 0)
			return false;
		
		$result = db_result_to_array($result);
		return $result;
	}
	
	
	
	
	function addrecord(){
?>

<td width="581" valign="top"><table width="100%" border="0" cellpadding="0" cellspacing="0">
		<tr> 
			<td valign="top" width="581" height="600"><form name="newrecord" method="post" action="addnewrecord.php?">
					<table>
						<font color="#000000" size="2" face="Courier New, Courier, mono">
							<tr>
								<td>Title: </td>
								<td><input name="title" type="text" maxlength="40" id="title"></td>
							</tr>
							<tr>
								<td>Artist: </td>
								<td><select name="artist_id">
										<?php
		$artist_array=get_artists();
		foreach ($artist_array as $thisartist)
		{
			echo '<option value="';
			echo $thisartist['artist_id'];
			echo '"';
			echo '>';
			echo $thisartist['artist_id'];
			echo '</option>';
			echo"\n";
		}
										?>
									</select>
								</td></table></form>	
						
						<div align="left"></div></td>
				</tr>
			</table></td>
	</tr>
</table>
<?
	}
?>
if you want to see any other code please ask. :x
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

mmc01ms wrote:

Code: Select all

if (!result)
turning on error_reporting(E_ALL); will probably hint you on stuffllike this:

missing a $ ......
Post Reply