Ask your WordPress questions! Pay money and get answers fast! (more info)

Gravity form data to database to HTML table WordPress

  • SOLVED

I am a newbie so forgive me if this is simple but I'm lost:

<strong>Quick Overview:</strong> I have the need to gather data from a Gravity form and then input that data into a database. The database data then needs to be displayed in an HTML table.

<strong>Detailed:</strong> The Gravity form is to gather information (Name, address, email, day of week, time slot) for a repeating bi-hourly time slot calender. There are to be time slots for every 30min increment of every day of the week (48 slots a day, 336 a week). As stated above this calender/time slot table would repeat weekly so if someone signed up for Monday at 6:00am then they would have that time slot indefinitely (or until they cancel). The user would select a time slot (day/time) from the form (drop down boxes) or via a "sign-up" link in one of the tables time slot boxes that would take them to the form and auto populate the drop down input fields with the corresponding day of week and time of day. Only the name of the persons signed up for a time slot would need to be displayed in the HTML table. There could also be multiple people signed up for each time slot. I almost forgot.....If its not too much to ask....I would also like to give the user the option to choose the timezone they are in and thus the HTML table would update. Here is a link to an example: [[LINK href="http://biglifeonline.org/prayerwall_view"]][[/LINK]]

I understand the process of grabbing the input fields from Gravity form by way of $_POST and assigning them to variables and then connecting to database:
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$address = $_POST['address'];
$day = $_POST['day'];
$time = $_POST['time'];

$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

mysql_query("INSERT INTO example_table (FirstName, LastName, Address, Day, Time)
VALUES ('$first_name', '$last_name', '$address' , 'day' ,'time')");

mysql_close($con);
?>

I'm just lost when it comes to pulling that info back out off the database and displaying it in an HTML table. I am also not positive on the best structure for the database table or tables to be in to pull this off.


****Not sure if I made this clear by the answers given so far but...... The data needs to be displayed in a table with a header of the days of the week (Sun-Sat) and the rows need to be broken down into the 48 half hour slots that make up each day. The cells that make up the table would display the people(s) that have signed up for the corresponding day & 1/2 hour slot. There WILL be multiple people per cell. Please look at the link that I attached above as this is exactly what I'm trying to achieve.****

Answers (6)

2011-08-25

ej_emman answers:

Hello,

Base on the data you have given I created a query to make you more comfortable.
Name, address, email, day of week, time slot


<?php
var $con;
function connection($server,$user,$pass)
{
Global $con;
$con = mysql_connect($server,$user,$pass) or die('connection failed');
return mysql_select_db("my_db", $con);
}

function my_query($sql)

{
$result = mysql_query($sql);
$arr =array();
while(mysql_fetch_array($result))
{
$arr[] = mysql_fetch_array($result);
}
return $arr;
}


connection("localhost","user","password");
$data = my_query("SELECT * FROM example_table");

//result table sample

$the_result = "<table>
<tr>
<th>Name</th>
<th>Address</th>
<th>Email</th>
<th>Days of Week</th>
<th>Time slot</th>
</tr>";
foreach($data as $table_result)
{
$the_result .= "<tr>
<td>$table_result['Name']</td>
<td>$table_result['Address']</td>
<td>$table_result['Email']</td>
<td>$table_result['DaysofWeek']</td>
<td>$table_result['Timeslot']</td>

</tr>";
}
$the_result .= "</table>";

echo $the_result;

mysql_close($con);
?>


BIG LIFE comments:

I appreciate everyone's responses thus far, but I don't think my notes are being read thoroughly. Please read the details about posting data from the database in a table where the header consists of Sunday-Saturday and the row Titles consist of the 48 half hour increments that make up each day. Please disregard the info about address and email....I think that just confused everyone. I just need the names of the people who signed up for a particular time slot on a particular day to show up in that corresponding cell on the table. I have posted a link to a site with the very item that I am trying to recreate. Please take a look at this to see what I'm going for exactly. Thanks again


ej_emman comments:

try this tell me if it helps..


<?php

var $con;

function connection($server,$user,$pass)

{
Global $con;
$con = mysql_connect($server,$user,$pass) or die('connection failed');
return mysql_select_db("my_db", $con);
}



function my_query($sql)

{
$result = mysql_query($sql);
return mysql_fetch_array($result);
}



connection("localhost","user","password");
$data = my_query("SELECT * FROM example_table ORDER BY time ASC");



//result table sample
$the_result = "<table>
<tr>
<th>Time</th>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>";
foreach($data as $result)
{
$the_result .= "<tr>".$result['time']."<td>";

$the_result .= "</td> <td>";
if($result['day'] == 'sunday')
{
$the_result .= $result['name'];
}

$the_result .= "</td> <td>";
if($result['day'] == 'monday')
{
$the_result .= $result['name'];
}


$the_result .= "</td> <td>";
if($result['day'] == 'tuesday')
{
$the_result .= $result['name'];
}


$the_result .= "</td> <td>";
if($result['day'] == 'wednesday')
{
$the_result .= $result['name'];
}


$the_result .= "</td> <td>";
if($result['day'] == 'thursday')
{
$the_result .= $result['name'];
}


$the_result .= "</td> <td>";
if($result['day'] == 'friday')
{
$the_result .= $result['name'];
}


$the_result .= "</td> <td>";
if($result['day'] == 'saturday')
{
$the_result .= $result['name'];
}


$the_result .= "</td></tr>";

}

$the_result .= "</table>";



echo $the_result;




mysql_close($con);

?>

2011-08-27

Jurre Hanema answers:

Why bother with saving the data to the database manually? Gravity Forms already saves every form entry to the DB, so it seems to me you are needlessy complicating things.

If you want to display the data later (say, in a HTML table), simply pull the data from Gravity Forms using either one of GF's (internal) functions or a custom DB query.

2011-08-23

akosipau answers:

need to put the mysql_query in a variable

ex.

$result = mysql_query(....);

then outputing result in HTML table

ex:

<table>
<tr>
<td>First Name</td>
<td>Last Name</td>
</tr>
while ($row = mysql_fetch_assoc($result)) { ?>
<tr>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
</tr>
<?php }
</table>

2011-08-24

Klian answers:

Hi Big Life,

To output the mysql data into a HTML table try with:

$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("my_db", $con);

$sql = "SELECT * FROM example_table";
$fire = mysql_query($sql) or die("<br><b>Error</b>: ".mysql_error()."<br><br><strong>SQL:</strong>:".$sql);

$output = '<table>
<tr>
<th>FirstName</th>
<th>LastName</th>
<th>Address</th>
<th>Day</th>
<th>Time</th>
</tr>
';
while($res = mysql_fetch_assoc($fire)){
$output .= '
<tr>
<td>'.$res['FirstName'].'</td>
<td>'.$res['LastName'].'</td>
<td>'.$res['Address'].'</td>
<td>'.$res['Day'].'</td>
<td>'.$res['Time'].'</td>
</tr>
';
}
$output .= '</table>';

mysql_close($con);

2011-08-24

MDan answers:

Hello Big Life,

First, you can find some related answers here on WPQ, the link is:

http://wpquestions.com/question/show/id/578

Check this video tutorial, it will be helpfull:

http://www.youtube.com/watch?v=VJQfZY0DeEQ

Also, check the link bellow:

http://www.seodenver.com/gravity-forms-addons/

2011-08-25

Aneesh Joseph answers:

i think you forgot to post the example link :)


BIG LIFE comments:

Oops my bad:
[[LINK href="http://biglifeonline.org/prayerwall_view"]][[/LINK]]


BIG LIFE comments:

http://biglifeonline.org/prayerwall_view