Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • Last Modified:

Loop thru MySQL and add values to PHP array

I have a audio file page on my website and I want to make it as easy as possible to update this so I made a php array so that all I have to do is add a new array into the script with the title, speaker, date, and file url in it and the script will automatically add the new player and text and file to the top of the list.

Now this works for me cause I don't mind typing in an array but to make it easier I was thinking of making a simple form with fields for those values and have it just insert into a table on my MySQL database. I have the form working great.

The problem is I do not know how to pass that data from the table to create a new array for my php script I attached my original script but need to make it work with MySQL database

I am a big newbie when it comes to MySQL and a novice with PHP so if you can explain your code it will help me learn, THANKS EXPERTS!
//this is my array script audio.php

<?php
$sermons = array(
	array('If He owns "The Cattle on  a Thousand Hills", What does he need my Money For?', "Tim Grosshans", "02-20-11", "file1.php"),
	array("The Power of Love", "Tim Grosshans", "02-13-11", "file2.php"),
	array("The Lord's Supper", "Tim Grosshans", "02-06-11", "file3.php"),
	array("The Big Picture", "Roger Palmer", "01-30-11", "file4.php"),
	array("I Just Forgot", "Tim Grosshans", "01-23-11", "file5.php"),
	array("Possible Gifts--Celibacy, Hospitality, Voluntary Poverty, Martyrdom", "Tim Grosshans", "01-16-11", "file6.php"),
);
?>

//then I have the php script that makes the looped list archive.php

<?php
include ('audio.php');
$size = count($sermons);
for ($message = 0; $message < $size; $message++)
{
	if($message % 2 == 0)
		{
	echo '<div style="background-color: #ffffff;">
<img class="alignleft" style="margin-top: 30px; margin-bottom: 10px;" title="Audio Message" src="http://XXXXXX/wp-content/uploads/2010/01/audio-message-image.png" alt="" width="90" height="90" />
<table style="width: 440px; height: 20px;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="2">
<h2 style="text-align: center;">'.$sermons[$message][0].'</h2>
</td>
</tr>
<tr>
<td><strong>'.$sermons[$message][1].'</strong></td>
<td>'.$sermons[$message][2].'</td>
</tr>
<tr>
<td><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="20" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://flash-mp3-player.net/medias/player_mp3.swf" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="mp3='.$sermons[$message][3].'&amp;bgcolor1=0b3975&amp;bgcolor2=072245&amp;buttoncolor=ffffff&amp;buttonovercolor=ffffff&amp;slidercolor1=ffffff&amp;slidercolor2=ffffff&amp;sliderovercolor=ffffff" /><param name="data" value="http://flash-mp3-player.net/medias/player_mp3.swf" /><embed type="application/x-shockwave-flash" width="200" height="20" src="http://flash-mp3-player.net/medias/player_mp3.swf" data="http://flash-mp3-player.net/medias/player_mp3.swf" flashvars="mp3='.$sermons[$message][3].'&amp;bgcolor1=0b3975&amp;bgcolor2=072245&amp;buttoncolor=ffffff&amp;buttonovercolor=ffffff&amp;slidercolor1=ffffff&amp;slidercolor2=ffffff&amp;sliderovercolor=ffffff" bgcolor="#ffffff"> </embed></object></td>
<td><a href="'.$sermons[$message][3].'" target="_blank">DOWNLOAD</a></td>
</tr>
</tbody>
</table>
</div>
<br />
<hr />';
}
	else {
echo '<div style="background-color: #ececec;">
<img class="alignleft" style="margin-top: 30px; margin-bottom: 10px;" title="Audio Message" src="http://XXXXXX/wp-content/uploads/2010/01/audio-message-image.png" alt="" width="90" height="90" />
<table style="width: 440px; height: 150px;" border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr>
<td colspan="2">
<h2 style="text-align: center;">'.$sermons[$message][0].'</h2>
</td>
</tr>
<tr>
<td><strong>'.$sermons[$message][1].'</strong></td>
<td>'.$sermons[$message][2].'</td>
</tr>
<tr>
<td><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="20" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="src" value="http://flash-mp3-player.net/medias/player_mp3.swf" /><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="mp3='.$sermons[$message][3].'&amp;bgcolor1=0b3975&amp;bgcolor2=072245&amp;buttoncolor=ffffff&amp;buttonovercolor=ffffff&amp;slidercolor1=ffffff&amp;slidercolor2=ffffff&amp;sliderovercolor=ffffff" /><param name="data" value="http://flash-mp3-player.net/medias/player_mp3.swf" /><embed type="application/x-shockwave-flash" width="200" height="20" src="http://flash-mp3-player.net/medias/player_mp3.swf" data="http://flash-mp3-player.net/medias/player_mp3.swf" flashvars="mp3='.$sermons[$message][3].'&amp;bgcolor1=0b3975&amp;bgcolor2=072245&amp;buttoncolor=ffffff&amp;buttonovercolor=ffffff&amp;slidercolor1=ffffff&amp;slidercolor2=ffffff&amp;sliderovercolor=ffffff" bgcolor="#ffffff"> </embed></object><br /></td>
<td><a href="'.$sermons[$message][3].'" target="_blank">DOWNLOAD</a></td>
</tr>
</tbody>
</table>
<br />
</div>
<hr />';
}
}
?>

Open in new window

0
John Grosshans
Asked:
John Grosshans
  • 3
1 Solution
 
TimBareCommented:
after you connect to your DB, use this function (changing ID,Artist,Title to rows in your table, and TableNameHere to your table name.

function fGetAllAudioFiles()
	{
	$query = "PREPARE GetAllAudioFiles0 FROM 'SELECT ID,Artist,Title FROM TableNameHere ORDER BY ID DESC'"; $result = mysql_query($query) or die(mysql_error());
	$query = "EXECUTE GetAllAudioFiles0"; $result = mysql_query($query) or die(mysql_error());
	$query = "DEALLOCATE PREPARE GetAllAudioFiles0"; $resultDeallocate = mysql_query($query) or die(mysql_error());
	$i = 0;
     	while ($row = mysql_fetch_array ($result, MYSQL_BOTH))
     		{
     		$allAudioFiles[$i][0] = $row['ID'];
     		$allAudioFiles[$i][1] = $row['Artist'];
			$allAudioFiles[$i][2] = $row['Title'];
     		$i++;
		}
	return $allAudioFiles;
	}

$AudioFileArray = fGetAllAudioFiles();

Open in new window

0
 
TimBareCommented:
btw, it's using a prepared statement for MySQL, which isn't really needed in this example, but is recommended when there's a possibility for user entry... If you want more info on that, let me know. I'd be happy share some more info on it...
0
 
TimBareCommented:
also, if you need help looping the multi-dimensional array, let me know...
0
 
John GrosshansDirector of TechnologyAuthor Commented:
AWESOME!!!!
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now