Solved

Loop thru MySQL and add values to PHP array

Posted on 2011-02-22
4
509 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
[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 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

724 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