Solved

Loop thru MySQL and add values to PHP array

Posted on 2011-02-22
4
479 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:JGrosshans
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
TimBare earned 250 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:JGrosshans
ID: 34963484
AWESOME!!!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now