Jump to content

help with arrays


dadamssg87

Recommended Posts

I have an array of dates and then i have an array for rates/prices for the days of the week. For example

<?php
$rate = array();
$rate['monday'] = 91.11;
$rate['tuesday'] = 92.22;
$rate['wednesday'] = 93.33;
$rate['thursday'] = 94.44;
$rate['friday'] = 105.55;
$rate['saturday'] = 106.66;
$rate['sunday'] = 90.00;

$dateMonthYearArr = array();
       $datemonthYearArr['0'] = "2011-05-06";
       $datemonthYearArr['1'] = "2011-05-07";  
       $datemonthYearArr['2'] = "2011-05-08";
       $datemonthYearArr['3'] = "2011-05-09";
       $datemonthYearArr['4'] = "2011-05-10";
       $datemonthYearArr['5'] = "2011-05-11";
       $datemonthYearArr['6'] = "2011-05-12";
?>

 

I have written this to sort through the $dateMonthYearArr to determine the dates day of the week

<?php
foreach ($dateMonthYearArr as $date) {
        $sum = 0;
$day = strtotime($date);
        $day_name = date("l", $day);
echo $day_name."<br>";
}

 

Now i'm trying to figure out how to work in the day rate array. I'd like to match up the $day_name of $dateMonthYearArr to the key of the rate array, grab that value and then add it to $sum.

Link to comment
Share on other sites

yes, it has to be in an array. Different price sets are stored in the database.  I use a function to query for a certain price set and return the values as an array. I'm using a mvc framework...thats why the syntax may look a little unfamiliar.

 

<?php

function get_rate_values($id)
   { 
$query = $this->db->query("SELECT * FROM Rates WHERE cal_id = '$id'");
$row = $query->row();
        $values['monday'] = $row->monday;
$values['tuesday'] = $row->tuesday;
$values['wednesday'] = $row->wednesday;
$values['thursday'] = $row->thursday;
$values['friday'] = $row->friday;
$values['saturday'] = $row->saturday;
$values['sunday'] = $row->sunday;
return $values;
   }

Link to comment
Share on other sites

Try doing something like this.

 

<?php

$rate = array();
$rate['monday'] = 91.11;
$rate['tuesday'] = 92.22;
$rate['wednesday'] = 93.33;
$rate['thursday'] = 94.44;
$rate['friday'] = 105.55;
$rate['saturday'] = 106.66;
$rate['sunday'] = 90.00;

$dateMonthYearArr = array();
       $dateMonthYearArr[] = "2011-05-06";
       $dateMonthYearArr[] = "2011-05-07";  
       $dateMonthYearArr[] = "2011-05-08";
       $dateMonthYearArr[] = "2011-05-09";
       $dateMonthYearArr[] = "2011-05-10";
       $dateMonthYearArr[] = "2011-05-11";
       $dateMonthYearArr[] = "2011-05-12";
     

$sum = 0;
foreach ($dateMonthYearArr as $date) {
$day = strtotime($date);
$day_name = strtolower( date("l", $day) );
$sum += $rate[$day_name];
// debug
echo "$date is a $day_name and {$rate[$day_name]} was added to the sum ($sum)<br />";
}

echo "Total sum: $sum";

?>

 

Edit - If you're going to ask for help PLEASE make sure we don't have to fix minor typing errors in your examples. $dateMonthYearArr != $datemonthYearArr

 

Also, you can simplify

 

$day = strtotime($date);

$day_name = strtolower( date("l", $day) );

 

To simply

 

$day_name = strtolower( date("l", strtotime($date) ) );

 

You save defining a variable, which though minuscule, saves memory. Good practice if you don't need to use the value later.

Link to comment
Share on other sites

Is this what you are looking for?

 

<?php

/*
	Written By: SMS Marketeers
	Website: http://www.smsmarketeers.com

	Using two arrays, use the name of the day to return the price for that day.

	Database Structure

	None
*/

?>

<html>
<head>
<title>Day Pricing using Arrays</title>
<style type="text/css">
	*		{ font-size:12px; font-family:Arial; margin:0px; outline:0px; padding:0px; }
	body	{ background:#ffffff; color:#000000; margin:10px 0px 0px 0px; }
	img		{ border:0px; }
	p		{ margin:5px 0px 10px 0px; }
	form	{ border:none; margin:0px; padding:0px; }

	a				{ cursor:pointer; }
	a:link			{ color:#9AB324; text-decoration:none; }
	a:visited		{ color:#9AB324; text-decoration:none; }
	a:hover 		{ color:#9AB324; text-decoration:underline; }
	a:active 		{ color:#9AB324; text-decoration:none; }

	.container		{ margin:0px auto; width:300px; }
	.success		{ background:#EEF5CD; border:1px dashed #9AB324; color:#608339; margin-bottom:5px; padding:5px 5px 5px 25px; text-align:left; }
	.warning		{ background:#eed4d2; border:1px dashed #a94637; color:#ac241a; margin-bottom:5px; padding:5px 5px 5px 25px; text-align:left; }
	.attention		{ background:#fefbcc; border:1px dashed #e6db55; color:#ada019; margin-bottom:5px; padding:5px 5px 5px 25px; text-align:left; }

	table.data th				{ background:#9AB324; border-bottom:1px solid #596E0E; color:#ffffff; font-weight:bold; padding:5px; text-align:center; }
	table.data td				{ border-bottom:1px solid #dddddd; padding:5px; }
	table.data td.rowOne		{ background:#f5f5f5; }
	table.data td.rowTwo		{ background:#eeeeee; }
	table.data td.button		{ background:#ffffff; border:none; text-align:right; }
</style>
</head>
<body>

<div class="container">
<?php

	$arrDays = array("sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday");
	$arrPrices = array("sunday" => "$1.00", "monday" => "$2.00", "tuesday" => "$3.00", "wednesday" => "$4.00", "thursday" => "$5.00", "friday" => "$6.00", "saturday" => "$7.00");

?>

<form action="" method="POST" name="form" id="form">
	<div class="attention">Using two arrays, use the name of the day to return the price for that day.</div>

	<table align="center" border="0px" cellpadding="0px" cellspacing="1px" class="data" width="300px">
		<tr>
			<th>Day of the Week</th>
			<th>Price</th>
		</tr>
		<?php

			$rowOne = 'rowOne';
			$rowTwo = 'rowTwo';
			$rowCount = 0;

		?>
		<?php foreach ($arrDays as $day) { ?>
			<?php $rowClass = ($rowCount % 2) ? $rowTwo : $rowOne; ?>
			<tr>
				<td class="<?php echo $rowClass; ?>"><?php echo ucwords($day); ?></td>
				<td class="<?php echo $rowClass; ?>"><?php echo $arrPrices[$day]; ?></td>
			</tr>
			<?php $rowCount++; ?>
		<?php } ?>
	</table>
</form>
</div>

</body>
</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.