Solved

How to do a loop to insert each value in an array to a new row in a table

Posted on 2013-02-04
2
299 Views
Last Modified: 2013-02-05
I have an array of datetimes that is within another array ($data):

Looks like:
$data[StartDates]

How can I insert each of the dates within StartDates into a table, 1 date per row?

Something like:

foreach ($data[StartDates] as $startdate) {
$query = "INSERT INTO contest_dates (contestdate) VALUES ($startdate) ";
$result = mysql_query($query) or die(mysql_error());
}

*the above fails
Message: Use of undefined constant StartDates - assumed 'StartDates'

Hope I'm explaining it right.
thanks
0
Comment
Question by:tjyoung
2 Comments
 
LVL 76

Accepted Solution

by:
arnold earned 500 total points
Comment Utility
Foreach ($data as $startdate) {
Might be what you are looking for/at.
http://php.net/manual/en/control-structures.foreach.php
You do not use a single element/array cell

If the on tents of $data[startdate] is a list than it is a differenent story.
Could you post an example of the data
$data[startdate]
$#data
0
 
LVL 1

Author Closing Comment

by:tjyoung
Comment Utility
Hi, answer was:
Foreach ($data['ContestStartDate'] as $value) {

but got me pointed in the right direction.
Thanks!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 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

15 Experts available now in Live!

Get 1:1 Help Now