/    Sign up×
Community /Pin to ProfileBookmark

output data stored in mysql table with php

hi experts,

i have a mysql table that holds information of events like name, date, location, etc. so each mysql table row is one event with its details.

now i want to output each event in its own html table (with 2 columns) that has some hardcoded labels like [B]Name[/B], [B]Date[/B], [B]Location[/B], etc in its left column and the respective data from the mysql table in the right column.

what i got so far is connecting to the db, selecting the table and echoing a message if there´s no event found.

[code=php]
<?php

/* Connecting, selecting database */

$link = mysql_connect(“localhost”, “root”, “”)
or die(“Error connecting to database”);
mysql_select_db(“mydatabase”) or die(“Database mydatabase not found.”); ?>

<h4 class=”events”>This is the Events Headline</h4>

<?php
$sql = “SELECT * FROM eventstable”;
$result = mysql_query($sql,$link);

/* Printing results in HTML */

if (! ($line = mysql_fetch_array($result))) {echo(“<br>”); echo (“Sorry, no events in the database”);}
else {THIS IS WHERE YOU SHOULD HELP ME PLEASE[/code]

thanks in advance,
jogol

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 10.2005 — [code=php]
<?php

/* Connecting, selecting database */

$link = @mysql_connect("localhost", "root", "") or die("Error connecting to database");
mysql_select_db("mydatabase") or die("Database mydatabase not found.");
?>

<h4 class="events">This is the Events Headline</h4>

<?php
$sql = "SELECT * FROM eventstable";
$result = mysql_query($sql,$link);
$numRows = mysql_num_rows($result);
/* Printing results in HTML */

if ($numRows == 0){
echo "<br>";
echo "Sorry, no events in the database";
}else{
# loop through results
for($i=0;$i<mysql_num_rows($result);$i++){
# print results
# mixed mysql_result ( resource result, int row [, mixed field])
# for example, the date
$date = mysql_result($result, $i, "date");
echo $date."<br>";
}
}

?>
[/code]
Copy linkTweet thisAlerts:
@jogolauthorOct 10.2005 — thanks LiLcRaZyFuZzY!

that works perfectly. what can i do to only display upcoming events and not those that already happened? my date field is of the type "date" (0000-00-00).

tia,

jogol
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 10.2005 — [code=php]
<?php

/* Connecting, selecting database */

$link = @mysql_connect("localhost", "root", "") or die("Error connecting to database");
mysql_select_db("mydatabase") or die("Database mydatabase not found.");
?>

<h4 class="events">This is the Events Headline</h4>

<?php
$sql = "SELECT * FROM eventstable";
$result = mysql_query($sql,$link);
$numRows = mysql_num_rows($result);
/* Printing results in HTML */

if ($numRows == 0){
echo "<br>";
echo "Sorry, no events in the database";
}else{
# loop through results
for($i=0;$i<mysql_num_rows($result);$i++){
# format yyyy-mm-dd
$date_today = date("Y-m-d");
# format yyyy-mm-dd
$date = mysql_result($result, $i, "date");
#if you dont want the events that will happen on the day of the request, simply write ">" instead of ">=" on the line below
if($date >= $date_today){
echo "$date<br>";
}
}
}

?>
[/code]
Copy linkTweet thisAlerts:
@jogolauthorOct 10.2005 — thanks again!
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 10.2005 — you are welcome ?
Copy linkTweet thisAlerts:
@chazzyOct 10.2005 — [code=php]<?php

/* Connecting, selecting database */

$link = @mysql_connect("localhost", "root", "") or die("Error connecting to database");
mysql_select_db("mydatabase") or die("Database mydatabase not found.");
?>

<h4 class="events">This is the Events Headline</h4>

<?php
$sql = "SELECT date FROM eventstable WHERE date > NOW()";
$result = mysql_query($sql,$link);
$numRows = mysql_num_rows($result);
/* Printing results in HTML */

if ($numRows == 0){
echo "<br>";
echo "Sorry, no events in the database";
}else{
# loop through results
for($i=0;$i<mysql_num_rows($result);$i++){
# print results
# mixed mysql_result ( resource result, int row [, mixed field])
# for example, the date
$date = mysql_result($result, $i, "date");
echo $date."<br>";
}
}

?> [/code]


Nice one, just wanted to point out that this code does exactly what the new version does, but only changes the query. It might also make sense to only get the date column since that seems to be all you're displaying...

Remember - speed is in the query, not the application.
Copy linkTweet thisAlerts:
@LiLcRaZyFuZzYOct 10.2005 — oh! nice one! thanks
Copy linkTweet thisAlerts:
@jogolauthorOct 10.2005 — i will be displaying more than the date. i will be displaying

date

title

location

start time

topic

info

speaker

contact information

homepage

email

entry fee

the problem is that not all fields are applicable at each event. so when the client adds a new event via a form and e.g. leaves the field "info" empty then the html table will still display the hardcoded row "info"
[code=html]
<tr>
<td align='left' valign='top'><strong>Info:</strong></td>
<td align='left' valign='top'>$info</td>
</tr>[/code]
although there is no data inside.

my code for now:

[code=php]
/* Printing results in HTML */
if ($numRows == 0){

echo "<br><table width='664' border='0' cellspacing='0' cellpadding='5px'>
<tr>
<td colspan='2'>No events found.</td>
</tr></table>";}

else {
for($i=0;$i<mysql_num_rows($result);$i++){
$date_today = date("Y-m-d");

$date = mysql_result($result, $i, "date");
$title = mysql_result($result, $i, "title");
$location = mysql_result($result, $i, "location");
$start = mysql_result($result, $i, "start");
$topic = mysql_result($result, $i, "topic");
$info = mysql_result($result, $i, "info");
$speaker = mysql_result($result, $i, "speaker");
$contact = mysql_result($result, $i, "contact");
$homepage = mysql_result($result, $i, "homepage");
$email = mysql_result($result, $i, "email");
$entry_fee = mysql_result($result, $i, "entry_fee");

if($date >= $date_today){
echo

"<br><table width='664' border='0' cellspacing='0' cellpadding='5px'>
<tr>
<td class='title' colspan='2'>$titel</td>
</tr>
<tr>
<td width='70' align='left' valign='top'><strong>Location:</strong></td>
<td width='530' align='left' valign='top'>$location</td>
</tr>
<tr>
<td align='left' valign='top'><strong>Date:</strong></td>
<td align='left' valign='top'>$date</td>
</tr>
<tr>
<td align='left' valign='top'><strong>Start:</strong></td>
<td align='left' valign='top'>$start</td>
</tr>
<tr>
<td align='left' valign='top'><strong>Topic:</strong></td>
<td align='left' valign='top'>$topic</td>
</tr>
<tr>
<td align='left' valign='top'><strong>Info:</strong></td>
<td align='left' valign='top'>$info</td>
</tr>
<tr>
<td align='left' valign='top'><strong>Speaker:</strong></td>
<td align='left' valign='top'>$speaker</td>
</tr>
<tr>
<td align='left' valign='top'><strong>Contackt:</strong></td>
<td align='left' valign='top'>$contact</td>
</tr>
<tr>
<td align='left' valign='top'><strong>Homepage:</strong></td>
<td align='left' valign='top'><a href='http://$homepage' target='blank'>$homepage</a></td>
</tr>
<tr>
<td align='left' valign='top'><strong>E-Mail:</strong></td>
<td align='left' valign='top'><a href='mailto:$email'>$email</a></td>
</tr>
<tr>
<td align='left' valign='top'><strong>Entry Fee:</strong></td>
<td align='left' valign='top'>$entry_fee</td>
</tr>
<tr>
<td colspan='2'><hr class='seperator'></td>
</tr>
</table>";
}
}
}
?>[/code]
Copy linkTweet thisAlerts:
@chazzyOct 10.2005 — jogol:

There are other approaches/functions you might want to look at:

1) if you issue the query "SELECT date,title,location,start_time,topic,info,speaker,contact_information,homepage,email,entry_fee from events where date > NOW()" you can look at the results via mysql_fetch_array($result,MYSQL_NUM). As it stands now, with all of your variables you have quite a lot of overhead on each iteration.

2) Learn cases Replace my query with "SELECT date,title,location,start_time,topic,

case info when null 'No Information available' else info "info" ,speaker,contact_information,homepage,email,entry_fee from events where date > NOW()" You'll need to double check the mysql syntax.

Now other than that, i'll update this more when i get to work.
Copy linkTweet thisAlerts:
@chazzyOct 10.2005 — well back to what i was saying.

review php.net's information mysql_result

http://us2.php.net/manual/en/function.mysql-result.php

basically, they point out that you should use one of the entire row getters when getting more columns (such as in your example)
Copy linkTweet thisAlerts:
@jogolauthorOct 11.2005 — thanks for your suggestions. i was reading a lot on php.net but as a total php noob it is kinda hard to use that information properly.

i was googling like mad to find a solution for dynamic <tr> and <td> generation to get rid of that bloated code i´m having and to avoid displaying a field that has no data inside.

unfortunately i didn´t become any wiser. ?
Copy linkTweet thisAlerts:
@chazzyOct 11.2005 — [code=php]
$resultset = mysql_query("SELECT date,title,location,start_time,topic,info,speaker,contact_information,homepage,email,entry_fee from events where date > NOW()");
while($thisrow = mysql_fetch_array($resultset,MYSQL_NUM)){
printf("<tr>");
foreach ( $thisrow as $colval ){
printf("<td>".$colval."</td>");
}//foreach
printf("</tr>");
}//while
[/code]


that should give you your desired results.
Copy linkTweet thisAlerts:
@jogolauthorOct 11.2005 — thanks for you efforts but your code displays all the fields left to right like this:

Event Name....|..Event Location..|..Date of Event..|..Event etc.
--------------------------------------------------------------


Event 1 Name.|..Event 1 Loc......|..Event 1 Date...|..Event 1 etc.

Event 2 Name.|..Event 2 Loc......|..Event 2 Date...|..Event 2 etc.

Event 3 Name.|..Event 3 Loc......|..Event 3 Date...|..Event 3 etc.

what i want to achieve is:

Event 1 Name (colspan="2")

Location .....|..Event 1 Loc.

Date...........|..Event 1 Date

Etc.............|..Event 1 etc.

Event 2 Name (colspan="2")

Location......|..Event 2 Loc.

Date...........|..Event 2 Date

Etc.............|..Event 2 etc.

Event 3 Name (colspan="2")

Location......|..Event 3 Loc.

Date...........|..Event 3 Date

Etc.............|..Event 3 etc.

damn this is tricky and i haven´t found a solution yet in the www :rolleyes:
Copy linkTweet thisAlerts:
@chazzyOct 11.2005 — so then just alter how it prints.

[code=php]$resultset = mysql_query("SELECT date,title,location,start_time,topic,info,speaker,contact_information,homepage,email,entry_fee from events where date > NOW()");
while($thisrow = mysql_fetch_array($resultset,MYSQL_NUM)){
printf("<table><tr><td colspan="2">Item</td></tr>");
printf("<tr><td>Date</td><td>".$thisrow[0]."</td></tr>n");
printf("<tr><td>Title</td><td>".$thisrow[1]."</td></tr>n");
printf("<tr><td>Location</td><td>".$thisrow[2]."</td></tr>n");
.....
printf("</table>");
}//while [/code]


and so on, so basically you need one of
[code=php]
printf("<tr><td>--FIELD DESCRIPTION--</td><td>".$thisrow[i]."</td></tr>n");
[/code]

for each item you want to display.

Let me know if you have any other questions.
Copy linkTweet thisAlerts:
@jogolauthorOct 16.2005 — thanks, but i still havent found a solution so that fields with no data in don´t get displayed. e.g. if there is no data in the field "etc":

Event 1 Name (colspan="2")

Location .....|..Event 1 Loc.

Date...........|..Event 1 Date

Etc.............|

should be

Event 1 Name (colspan="2")

Location......|..Event 1 Loc.

Date...........|..Event 1 Date
Copy linkTweet thisAlerts:
@chazzyOct 16.2005 — thanks, but i still havent found a solution so that fields with no data in don´t get displayed. e.g. if there is no data in the field "etc":

Event 1 Name (colspan="2")

Location .....|..Event 1 Loc.

Date...........|..Event 1 Date

Etc.............|

should be

Event 1 Name (colspan="2")

Location......|..Event 1 Loc.

Date...........|..Event 1 Date[/QUOTE]


Use this:
[code=php]
if(strlen($thisrow[i]) > 0){
printf("<tr><td>--FIELD DESCRIPTION--</td><td>".$thisrow[i]."</td></tr>n");
}
[/code]


now it'll only print the row if there is data in it!
Copy linkTweet thisAlerts:
@jogolauthorOct 17.2005 — cool, thanks for your efforts chazzy!
Copy linkTweet thisAlerts:
@chazzyOct 17.2005 — No problem.

Just want to warn you, the code gets very tedious after like 10 columns :-P
×

Success!

Help @jogol spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...