Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Loop thru MySQL and add values to PHP array

Posted on 2011-02-22
4
Medium Priority
?
523 Views
Last Modified: 2012-05-11
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
Comment
Question by:John Grosshans
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
TimBare earned 1000 total points
ID: 34954581
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
 
LVL 2

Expert Comment

by:TimBare
ID: 34954597
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
 
LVL 2

Expert Comment

by:TimBare
ID: 34954612
also, if you need help looping the multi-dimensional array, let me know...
0
 

Author Closing Comment

by:John Grosshans
ID: 34963484
AWESOME!!!!
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question