Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Entering Array Key and Value into Table

Featured Replies

Still struggling with this so will try a different tact:

 

I want to enter the month and the associated key into the table from the following drop down (into a different column in the db).

 

This is what I have so far which enters the month as words only, but I'm not sure how to enter the key too:

 

function make_month_pulldown() {

// Make the months array:
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

// Make the months pull-down menu:
echo '<select name="month">';
foreach ($months as $key => $value) {
	echo '<OPTION value='.$value.'> '.$value.'';
}
echo '</select>';
}

 

There's a bit more validation but you get the idea of entering to the database:

if (isset($_POST['submitted'])) {
$mo = mysqli_real_escape_string ($dbc, $trimmed['month']);
}
$q = "INSERT INTO gigs (venue, month, month_id, day, band_name, price, on_the_door, tickets_from) VALUES ('$v', '$mo', '$mid' '$da', '$bn', '$pr', '$otd', '$tf' )";

 

Thanks for any help ...

  • Author

Thanks Jamest for that fast responce.

 

Where do I insert this code? I'm getting errors saying Undefined Variable: months....

 

Thanks again?

Still struggling with this so will try a different tact:

 

I want to enter the month and the associated key into the table from the following drop down (into a different column in the db).

 

This is what I have so far which enters the month as words only, but I'm not sure how to enter the key too:

 

function make_month_pulldown() {

// Make the months array:
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

// Make the months pull-down menu:
echo '<select name="month">';
foreach ($months as $key => $value) {
	echo '<OPTION value='.$value.'> '.$value.'';
}
echo '</select>';
}

 

There's a bit more validation but you get the idea of entering to the database:

if (isset($_POST['submitted'])) {
$mo = mysqli_real_escape_string ($dbc, $trimmed['month']);
}
$q = "INSERT INTO gigs (venue, month, month_id, day, band_name, price, on_the_door, tickets_from) VALUES ('$v', '$mo', '$mid' '$da', '$bn', '$pr', '$otd', '$tf' )";

 

Thanks for any help ...

 

Try changing your function so that the numeric value of the month ($key) is placed in the value attribute of the option element.

 

eg.

echo "<OPTION value='".$key."'> ".$value."</OPTION>";

 

To capture this value when the form is submitted use something like:

$iMonth = $_POST['month'];
//to get the name of the month you could try this
$strMonth =  date("F",strtotime("December 2008 +".$_POST['month']."months"));

//then insert the values into your database...

  • Author

I think I am even more confused now (but thank you CityWebConsultants!). Here is all the code for the page which may make it a little clearer ...

 

<?php # Update Gigs
session_start(); 

if (!isset($_SESSION['user_id'])) {
require_once ('includes/login_functions_a.inc.php');
$url = absolute_url();
header("Location: $url");
exit();	
}
require_once ('includes/config.inc.php');
$page_title = 'Add Gigs';


// This function makes a pull-down tickets from menu 
function make_tickets_pulldown() {

// Make the tickets from array:
$tickets_from = array (1 => '', 'the venue', 'Music Mania');
// Make the venue pull-down menu:
echo '<select name="tickets_from">';
foreach ($tickets_from as $key => $value) {
	echo '<OPTION value="'.$value.'"> '.$value.'</option>';  // this causes the genera to be included rather than the key position
}
echo '</select>';

}

// This function makes a pull-down on_the_door/pre sale menu 
function make_on_the_door_pulldown() {

// Make the tickets from array:
$on_the_door = array (1 => '', 'On the Door', 'Pre Sale');
// Make the venue pull-down menu:
echo '<select name="on_the_door">';
foreach ($on_the_door as $key => $value) {
	echo '<OPTION value="'.$value.'"> '.$value.'</option>';  // this causes the genera to be included rather than the key position
}
echo '</select>';
}

// This function makes a pull-down venue menu 
function make_venue_pulldown() {

// Make the venues array:
$venue = array (1 => '', 'Fatcats', 'The Glebe', 'Riggers', 'Satchmos', 'The Sugarmill', 'The Underground');
// Make the venue pull-down menu:
echo '<select name="venue">';
foreach ($venue as $key => $value) {
	echo '<OPTION value="'.$value.'"> '.$value.'</option>';  // this causes the genera to be included rather than the key position
}
echo '</select>';
}

// This function makes a pull-down DAY menu 
function make_day_pulldown() {
// Make the day array:
$day = array (1 => '', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9', '10', '11', '12', '13',  '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31');
// Make the dAY pull-down menu:
echo '<select name="day">';
foreach ($day as $key => $value) {
	echo '<OPTION value='.$value.'> '.$value.'';  // this causes the genera to be included rather than the key position
}
echo '</select>';
}

// This function makes three pull-down menus
// for selecting a month, day, and year.
function make_month_pulldown() {

// Make the months array:
$months = array (1 => 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');

// Make the months pull-down menu:
echo '<select name="month">';
foreach ($months as $key => $value) {
//		echo '<OPTION value='.$value.'> '.$value.'';
	echo "<OPTION value='".$key."'> ".$value."</OPTION>";

}

echo '</select>';
}

// Check if the form has been submitted:
if (isset($_POST['submitted'])) {

require_once (MYSQL); // Connect to the db.

// Trim all the incoming data:
$trimmed = array_map('trim', $_POST);

	// Assume invalid values:
$bn = $da = $mo = $v = $pr = $otd = $tf = FALSE;

// Check for a band name assumes only letters:
if (preg_match ('/^[A-Z0-9 \'.-\\\]{2,100}$/i', $trimmed['band_name'])) {
	$bn = mysqli_real_escape_string ($dbc, $trimmed['band_name']);
} else {
	echo '<p class="error">Please enter the band\'s name</p>';
}

// Check for a month:
if (preg_match ('/^\w{1,10}$/', $trimmed['month'])) {
	$mo = mysqli_real_escape_string ($dbc, $trimmed['month']);
} else {
	echo '<p class="error">Please enter a valid month</p>';		
}

	// Check for a year:
if (preg_match ('/^\w{1,2}$/', $trimmed['day'])) {
	$da = mysqli_real_escape_string ($dbc, $trimmed['day']);
} else {
	echo '<p class="error">Please enter a valid date</p>';
}

// Check for venue assumes only letters:
if (preg_match ('/^[A-Z0-9 \'.-\\\]{2,20}$/i', $trimmed['venue'])) {
	$v = mysqli_real_escape_string ($dbc, $trimmed['venue']);
} else {
	echo '<p class="error">Please enter the venue</p>';
}		



if (preg_match ('/^\d\.{1,5}$/', $trimmed['price'])) {
	$pr = mysqli_real_escape_string ($dbc, $trimmed['price']);
} else {
	$pr = mysqli_real_escape_string ($dbc, '999');
}

// Check for on the door  :
if (preg_match ('/^[A-Z0-9 \'.-\\\]{0,100}$/i', $trimmed['on_the_door'])) {
	$otd = mysqli_real_escape_string ($dbc, $trimmed['on_the_door']);
}	

// Check for tickets from :
if (preg_match ('/^[A-Z0-9 \'.-\\\]{0,100}$/i', $trimmed['tickets_from'])) {
	$tf = mysqli_real_escape_string ($dbc, $trimmed['tickets_from']);
}

if ($mo && $bn && $da && $v) { // If everything's OK...



	// Make the query:
	$q = "INSERT INTO gigs (venue, month, day, band_name, price, on_the_door, tickets_from) VALUES ('$v', '$mo' '$da', '$bn', '$pr', '$otd', '$tf' )";		
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
	if (mysqli_affected_rows($dbc) == 1) {



		// Print a message:
		echo '<h1>Thank you</h1>
	<p>Gig added. You may continue to add further gigs below.</p><p><br /></p>';	

	} else { // If it did not run OK.

		// Public message:
		echo '<h1>System Error</h1><p class="error">You could not be registered due to a system error. We apologize for any inconvenience.</p>'; 
		}
	}
}
?>
<h1>Add a Gig</h1>

<form action="add_gigs.php" method="post">
<fieldset>
<p><b>Band Name:</b> <input type="text" name="band_name" size="20" maxlength="100" value="" /></p>
<p>Month:  <?php make_month_pulldown();  ?> </p>
<p>Date:  <?php make_day_pulldown();  ?> </p>
<p>Venue:  <?php make_venue_pulldown();  ?> </p>
<p><b>Price:</b> <input type="text" name="price" size="5" maxlength="5" value="" /></p>
<p>Pre Sale?:  <?php make_on_the_door_pulldown();  ?> </p>
<p>Tickest From:  <?php make_tickets_pulldown();  ?> </p>
<p></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Add Gig" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>

The array of months is currently creates a drop down containing the month name as text, and the month name as a value.

 

If you view the generated code in a browser it will look like this:

 

<select name ="month">

<option value ="January">January</option>

etc....

</select>

 

It would be better to create the drop down like this:

<select name ="month">

<option value ="1">January</option>

etc....

</select>

 

The month number can be derived by using $key in your foreach loop. (see my previous post)

 

If the month number is returned then you can work out the month name using

date("F",strtotime("December 2008 +".$_POST['month']."months"));

 

This uses the stringtotime function - great for calculating dates :)

If the month selected was "March" then the value of $_POST['month'] would be 3.

In the case of "March" then, the stringtotime function is calculating December 2008 + 3months

 

$iMonth = $_POST['month']; // this should equal 3 if March is selected

//to get the name of the month you could try this

$strMonth = date("F",strtotime("December 2008 +".$_POST['month']."months"));//this should return March

 

Try echoing this out when the form gets submitted:

 

eg:

echo $_POST['month'];

echo date("F",strtotime("December 2008 +".$_POST['month']."months"));

 

Hope this helps.

  • Author

I think I can get the value to enter into the database as either a number of the month in words so I think the issue for me now is trying to display this in the 'gigs; page. Here the months are meant to be displayed once with the ilst of dates below for each gig.

 

I think this is where I need to use the date("F",strtotime("December 2008 +".'month'."months")); function.

 

Here is my page:

 

require_once (MYSQL); // Connect to the db.

$q = "SELECT month, day, band_name, venue, price, tickets_from, on_the_door FROM gigs ORDER BY month, day";
$r = mysqli_query($dbc, $q);		

$currentMonth = '';

while ($messages = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

   if ($currentMonth != $messages['month']) {

       echo "<h2>{$messages['month']}</h2>\n";
       $currentMonth = $messages['month'];
   }

   echo "    
{$messages['day']}   
".stripslashes($messages['band_name'])."
, ".stripslashes($messages['venue'])." ";



if($messages['price']=='0'){ 
echo ", FREE";
}
if($messages['price']=='999'){ 
echo "";
}

elseif($messages['price']!=null){ 
echo ", £{$messages['price']}";
} 
if($messages['on_the_door']!=null){ 
echo " ".stripslashes($messages['on_the_door'])."";
} if($messages['tickets_from']!=null){ 
echo ", get tickets at ".stripslashes($messages['tickets_from'])." ";
}  
echo " <br /><br />\n";

}
mysqli_free_result ($r); // Free up the resources.	
mysqli_close($dbc); 

  • Author

Phew. That took some time ...

 

Thanks CWC, got there with your last bit of code ...

 

Phew again,

 

Big grins,

Smileyboy

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.