Forum Moderators: coopster

Message Too Old, No Replies

Need a new line of DB table to display each week of year

This is my first try at a working database

         

M_Appel

11:04 pm on Jan 24, 2008 (gmt 0)

10+ Year Member


I am trying to create a site that has one devotion showing for each week of the year. I have been looking for weeks to try to find something that will work for me. I have learned a lot but I can't seem to put it all together. I am now up against a deadline so I really hope that someone can help me. I may just need to start from scratch. I don't know. Right now I just keep getting error messages.
Thanks for your help.

Here is the really messed up code that I have so far:

<?php
// Request one devotion

$query = "SELECT * FROM devo ORDER BY devo.id ASC LIMIT 1";
$result = mysql_query($query);
$i=0;
if($num=mysql_numrows($result))
where ($i < $num) {

//$variable=mysql_result($result,$i,"fieldname");
$id=mysql_result($result,$i,"id");
$title=mysql_result($result,$i,"devotitle");
$week=mysql_result($result,i,"devoweek");
$verse=mysql_result($result,$i,"devoverse");
$text=mysql_result($result,$i,"devotext");
$read=mysql_result($result,$i,"devoread");

//Get current week as a number
$week=date('W')
$j=0;
if($week($result))
while ($j < $week){

echo "
$row->ID <h2>$row->title</h2>
<h6>Week $row->week</h6>
<h4>$row->verse</h4>
<p>$row->text</p>
<p>&nbsp;</p>
<h3>Recommended reading - $row->verse</h3>
<br />\n";

}

$i++;
}

?>

eelixduppy

2:39 am on Jan 25, 2008 (gmt 0)



>> Right now I just keep getting error messages.

What are the error messages you are receiving?

You might also want to take a look at the following thread: [webmasterworld.com...]

btw, Welcome to WebmasterWorld! :)

M_Appel

2:59 am on Jan 25, 2008 (gmt 0)

10+ Year Member


Thanks for responding so quickly. I gotten to the point where about all I can do is stare at the screen.

This is the error that I am getting:

Parse error: syntax error, unexpected T_VARIABLE in C:\inetpub\wwwroot\mh_devotions_test_3.php on line 142

line 142: if($week($result))

eelixduppy

9:32 am on Jan 25, 2008 (gmt 0)



I'm not even sure what you are trying to accomplish with that line of code, but that syntax is definitely not correct nor does it make any sense. Have you taken a look at the thread that I linked to above? It should give you a very good template of what it should look like. All you'd have to do is change a few things around for it to work with your application.

M_Appel

8:26 pm on Jan 25, 2008 (gmt 0)

10+ Year Member


Thanks. Yes I did look at it but it didn't look like the display was controlled by a date. I can make one devotion show o.k. but I don't know how to make it change for the next week. It just gets really messed upp when I try to add the code for changing the date.

Here is what I was using to make it display one devotion.

// Connect to the server daqtabase server

$dbcnx = @mysql_connect('$server', '$username', '$password');
if (!$dbcnx) {
exit('<p>Unable to connect to the ' . 'database server at this time.</p>');
}

if (!@mysql_select_db('life')) {
exit('<p>Unable to locate the Life ' .
'database at this time.</p>');
}
?>
<?php

// Request the titles of all the devotions
$result = @mysql_query('SELECT devotitle, devoweek, devotext, devoverse, devoread FROM devo ORDER BY devo.id ASC LIMIT 1');
if (!$result) {
exit('<p>Error performing query: ' .
mysql_error() . '</p>');
}

// Display the title of each devotion in a paragraph
while ($row = mysql_fetch_object($result)) {
echo "
$row->ID
<h2>$row->devotitle</h2>
<h6>Week $row->devoweek</h6>
<h4>$row->devoverse</h4>
<p>$row->devotext</p>
<p>&nbsp;</p>
<h3>Recommended reading - $row->devoverse</h3>

<br />\n";
}
?>

M_Appel

7:22 am on Jan 26, 2008 (gmt 0)

10+ Year Member



Just to let you know: These are some of the things that I've tried & haven't been able to make work.
_____________________________________________________________________

$i = 0;
While ($i <= 53) {

"SELECT $i";
$i++;
}
while ($get_info = mysql_fetch_row($result))
{
foreach ($get_info as $field)
$id=mysql_result($result,$i,"id");
$title=mysql_result($result,$i,"devotitle");
$week=mysql_result($result,i,"devoweek");
$verse=mysql_result($result,$i,"devoverse");
$text=mysql_result($result,$i,"devotext");
$read=mysql_result($result,$i,"devoread");
echo "$field\n\n";
echo "\n";
}
Error:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 54 on MySQL result index 2 in C:\inetpub\wwwroot\mh_devotions_test_4.php on line 148

This is line 148:
$title=mysql_result($result,$i,"devotitle");
_____________________________________________________________________

$query = "SELECT * FROM devo WHERE id=$week_num";
$result = mysql_query($query);
$week_num = date('W');
while($row = mysql_fetch_row( $result )) {
echo"<h2>"$row['devotitle']"</h2>";
echo"<h6>"$row['Week devoweek']"</h6>";
echo"<h4>"$row['devoverse']"</h4>";
echo"<p"$row['devotext']"</p>";
echo"<p>&nbsp;</p>";
echo"<h3>Recommended reading - "$row['devoread']"</h3>";
echo"<br />\n";
}
Error:
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in C:\inetpub\wwwroot\mh_devotions_test_3.php on line 145

This is line 145:
echo"<h6>"$row['Week devoweek']"</h6>";
_____________________________________________________________________

<?php
// Request one devotion

$query = "SELECT * FROM devo ORDER BY devo.id ASC LIMIT 1";
$result = mysql_query($query);
if($num=mysql_numrows($result))

$i=0;
while ($i < 53) {
$i++;

$title=mysql_result($result,$i,"devotitle");
$week=mysql_result($result,i,"devoweek");
$verse=mysql_result($result,$i,"devoverse");
$text=mysql_result($result,$i,"devotext");
$read=mysql_result($result,$i,"devoread");

// Display each devotion in a paragraph
echo "
<h2>$title</h2>
<h6>Week $week</h6>
<h4>$verse</h4>
<p>$text</p>
<p>&nbsp;</p>
<h3>&nbsp;$read</h3>";
}
ERROR:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 2 in C:\inetpub\wwwroot\mh_devotions_test_1.php on line 134

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 2 in C:\inetpub\wwwroot\mh_devotions_test_1.php on line 136

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 2 in C:\inetpub\wwwroot\mh_devotions_test_1.php on line 137

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 1 on MySQL result index 2 in C:\inetpub\wwwroot\mh_devotions_test_1.php on line 138
Week 1

These are the lines:
133 $title=mysql_result($result,$i,"devotitle");
134 $week=mysql_result($result,i,"devoweek");
135 $verse=mysql_result($result,$i,"devoverse");
136 $text=mysql_result($result,$i,"devotext");
137 $read=mysql_result($result,$i,"devoread");
138
_____________________________________________________________________

I hope this isn't too much to post all at once but I wanted you to know that I have been working on it and just am not getting something.

Please HELP!? Thanks.

jatar_k

12:49 pm on Jan 26, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you want each id assigned to a different week you could use date('W') [php.net]to get the week number and then pass that to your select.

are you going to use a drop down for date?

do you aready have the date selection part figured out?

you can pass the date function a timestamp to get the week number for that time

M_Appel

6:54 pm on Jan 26, 2008 (gmt 0)

10+ Year Member


>>if you want each id assigned to a different week you could use >>date('W')to get the week number and then pass that to your select.

I thought that what I was doing that with this code but, again, it didn't work.

$query = "SELECT * FROM devo WHERE devoweek=$week_num";
$week_num = date('W');

I am adding 52 devotions to the database so the id's would be 01-53 but I have also added a field called devoweek where the numbers would be 01-53. I was hoping to match the current date(W) to either the id or the devoweek to make it display the correct week.

>>are you going to use a drop down for date?

Right now I don't have an actual date in the database itself

>>do you aready have the date selection part figured out?
<p>I thought that date(W) would work but apparently not</p>

<blockquote>you can pass the date function a timestamp to get the week number for that time

I did try that and I get a neverending loop where devoweek 1 repeats itself down the page. Here is the code for that:

<?php
// Request devotion

$query = "SELECT * FROM devo";
$result = mysql_query($query);
$week = date('W', $timestamp);

$devo_week = 'devoweek';
$row = mysql_fetch_object($result);
while ($week=$devo_week){
echo "
<h2>$row->devotitle</h2>
<h6>Week $row->devoweek</h6>
<h4>$row->devoverse</h4>
<p>$row->devotext</p>
<p>&nbsp;</p>
<h3>Recommended reading - $row->devoread</h3>

<br />\n";
}
?>

M_Appel

9:03 am on Jan 27, 2008 (gmt 0)

10+ Year Member


It really seems like this should work but it returns empty.

<?php
// Request all devotions
$query = "SELECT * FROM devo";
$result = mysql_query($query);
$row = mysql_fetch_row($result);

//define variables
$week_num = strftime("%W");
$devo_week = 'devoweek';

//while condition is true display on screen
while ($week_num == $devo_week){
echo"
<h2>$row->devotitle</h2>
<h6>Week $row->devoweek</h6>
<h4>$row->devoverse</h4>
<p>$row->devotext</p>
<p>&nbsp;</p>
<h3>Recommended reading - $row->devoread</h3>
<br />\n";
}
?>

Esqulax

4:08 pm on Jan 27, 2008 (gmt 0)

10+ Year Member




Ok, youve defined $devo_week as 'devoweek' which, if im not mistaken, is a string.
Looks like your comparing the week number to that string.
What is devoweek? is that an elemnt in your database? cos if so, shouldnt that be something like $row['devoweek']

normally (for me anywys) when the code returns emptyness, the code is coo, but th values passed/manipulated therin are awry
2 cents... idle observation from an ameteur

M_Appel

11:29 pm on Jan 27, 2008 (gmt 0)

10+ Year Member



Thanks. I did try that but nothing is showing up yet.

I am running out of time because I have to have this site finished by this Wednesday. I hate to just give up but I don't have that luxury anymore.

Does anyone know of someone that I can hire to create this code for me?

Please HELP?!

jatar_k

12:12 am on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I tested this with a database of mine but then plugged it into your latest code, it works but I am piecing together info from a bunch of posts, you may have to adjust it.

the logic works though

<?php
// Request all devotions
$query = "SELECT * FROM devo";
$result = mysql_query($query);
$devoarr = array();
while ($row = mysql_fetch_array($query)) {
$devoarr[] = array($row['devotitle'],$row['devoweek'],$row['devoverse'],$row['devotext'],$row['devoread']);
}
// get week number and multiply to get rid of the
// leading 0 and -1 to fit with the array
// which should be 0 to 52
$week_num = (strftime("%W") * 1) - 1;

// use week number to access proper element of devoarr
echo'<h2>',$devoarr[$week_num][0],'</h2><h6>Week ',$devoarr[$week_num][1],'</h6><h4>',$devoarr[$week_num][2],'</h4><p>',$devoarr[$week_num][3],'</p><p>&nbsp;</p><h3>Recommended reading - ',$devoarr[$week_num][4],"</h3><br />\n";
?>

I built the result set into an array and then just used the weeknum to access the appropriate element of the array.

oh, and you don't need to hire someone, you're close and you're trying so we like to help those that are working to help themselves ;)

M_Appel

3:21 am on Jan 28, 2008 (gmt 0)

10+ Year Member


Thank you so much. I was really getting desperate. I tried the code you gave me but I got this error. Does that mean that I have something wrong with the database itself since it worked for you?

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\inetpub\wwwroot\mh_devotions_test_10.php on line 127

Week


Recommended reading -

M_Appel

3:29 am on Jan 28, 2008 (gmt 0)

10+ Year Member


Sorry. I forgot to add the lines that were referenced in the error message.

126: while ($row = mysql_fetch_array($query)) {
$devoarr[] =
127: array($row['devotitle'],$row['devoweek'],$row['devoverse'],$row['devotext'],$row['devoread']);
128: }

jatar_k

3:35 am on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it's a patching together error, try this

<?php
// Request all devotions
$query = "SELECT * FROM devo";
$result = mysql_query($query);
$devoarr = array();
while ($row = mysql_fetch_array($result)) {
$devoarr[] = array($row['devotitle'],$row['devoweek'],$row['devoverse'],$row['devotext'],$row['devoread']);
}
// get week number and multiply to get rid of the
// leading 0 and -1 to fit with the array
// which should be 0 to 52
$week_num = (strftime("%W") * 1) - 1;

// use week number to access proper element of devoarr
echo'<h2>',$devoarr[$week_num][0],'</h2><h6>Week ',$devoarr[$week_num][1],'</h6><h4>',$devoarr[$week_num][2],'</h4><p>',$devoarr[$week_num][3],'</p><p>&nbsp;</p><h3>Recommended reading - ',$devoarr[$week_num][4],"</h3><br />\n";
?>

M_Appel

4:45 am on Jan 28, 2008 (gmt 0)

10+ Year Member



Woo Hoo! That worked. Thank you sooo much. I see the difference was $result instead of array in the while statement.

You have no idea how long I have been trying to work this out. I really had no idea what I was getting into when I said that I would learn this so she could have her devotions.

I knew that I wanted to learn it anyway but I see now that this will take more than a few months to get the hang of it. There are so many choices; I didn't know whether to use mysql_fetch_array or object or row and it seemed every time you changed it all the syntax changed with it.
I was way over my head and I really appreciate you helping me out.

Really, I would be glad to pay you. It is so worth it to know that it is done. Thank you again.

ChainsawXIV

5:46 am on Jan 28, 2008 (gmt 0)

10+ Year Member



Incidentally, if you're interested in form and efficiency (or just seeing another way to approach this problem), you were on a good track several posts up when you talked about trying the following:

$query = "SELECT * FROM devo WHERE devoweek=$week_num"; 
$week_num = date('W');

Your problem in that snippet was that you tried to use

$week_num
before you had actually defined it. Following on that line of thinking though, you could write a simpler bit of code to achieve the same results as the code in Jatar's post, by letting MySQL do most of the work for you. It's gentler on your server (not a big deal, but why not), and nice and tidy to boot:

<? 

// Get the week number as a number (1-53)
// You can subtract one if you need (0-52)
// Multiplying by one to drop the leading 0
$week_num = strftime("%W") * 1;

// Make a query which searches for the right week
// You were on the right track a couple posts ago
// But you had defined $week_num after defining $query
$query = "SELECT * FROM `devo` WHERE `devoweek` = $week_num";
$result = mysql_query($query);

// Make your associative array automatically
// No need to do it the hard way, as long as you
// Can use your column names as the array keys
$devoarr = mysql_fetch_assoc [us2.php.net]($result);

// Just call out the proper column name for each spot
// Also added in some line breaks (\n) for pretty code
echo "<h2>".$devoarr['devotitle']."</h2>\n
<h6>Week ".$devoarr['devoweek']."</h6>\n
<h4>".$devoarr['devoverse']."</h4>\n
<p>".$devoarr['devotext']."</p>\n
<p>&nbsp;</p>\n
<h3>Recommended reading - ".$devoarr['devoread']."</h3>\n
<br />\n";

?>

jatar_k

12:53 pm on Jan 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you were definitely on the right track, your logic was right, you just had some basic code issues in each example.

happy to help