Right this i what i'm trying to do, it's pretty simple but for some reason doesn't want to work and I can't figure out why!?
I'm trying to create a form using select. There are a lot of options so i want to keep them out side the main file and use:include ('options.inc'); to access them. I've put them in a variable like this:&var = "<option>***</option>
<option>***</option>
<option>***</option>
<option>***</option>
<option>***</option>";
....and so onI then run the form something like this:echo '<form action="****.php"><select name="****">' . $var . '</select></form>';if the variable is declared in the main file it runs just fine, but as soon as i try too be cleaver and move out to be included it doesn't work.:confused:
Second, you might want to consider making the included file a .php file. You know, options.php . And since you're using single quotes for the string, your double quotes for the html don't need to be escaped.
Durbs
08-16-2004, 03:00 PM
The & was just my useless post typing fingers doing their best to annoy me ($ in actual code). I've tried using *.php to no avail, any other ideas?
MstrBob
08-16-2004, 03:05 PM
Hmm, lets see. You put the include first, it's a .php file... Did you put the necessary <? ?> tags around it, like all good PHP files?
Durbs
08-16-2004, 03:08 PM
yep it goes a little something like this:<?php
$var = "<option>***</option>
<option>***</option>
<option>***</option>
<option>***</option>
<option>***</option>";
....and so on
?>Is that wot you mean?
MstrBob
08-16-2004, 03:11 PM
Curious, it works for me. You don't get any inclusion errors? If your path is right, its .php, you've got the necessary tags, and the include is before the echo, then I don't see what's wrong.
Durbs
08-16-2004, 03:27 PM
I've fixed it now, forgot a *%%*(& *((&@#*^(^$"£$! little ;!! Sorry to waste your time MstrBob, i checked so many time for the correct syntax - don't know how i missed it.
Durbs
08-16-2004, 05:35 PM
The above code was for a date (as in time and date, not the other date which involve flowers - and if you follow my methods - copious amounts of alchol) selection form. In other words users can selet a day (1-31), a month (jan-dec) and the year from drop down boxs. Is their a better way of doing this than wot follows? I want the years to display no higher than the current year, yet when we come round to 2005, display that to with out the need of...me.<?php
//sets the options for the for date selection
//sets the options for the days
$date_days = "<option>01</option>
<option>02</option>
<option>03</option>
<option>04</option>
<option>05</option>
<option>06</option>
<option>07</option>
<option>08</option>
<option>09</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
<option>31</option>";
For the years would it be best to include the options for years to come and try to control how many options are show to the user?
To stop users selecting, for example, 30th of feb i plan to use an if statement that just checks that when feb is selected the day can be no more than 28 (and the same for other April, june, sept & nov which only have 30). The only thing is that this year is a leap year (or at least i think it is) so wot do i do about that, just ignore it?
Nehow as i'm definitely not a PHP expert just wanted to hear everyones thoughts on this while i work on the idea/problem...
MstrBob
08-16-2004, 08:29 PM
Well, for giving date selections, here's what I used (with modifications for your date requirements):
Now, I would use some Javascript to stop users from selecting something like February 30, with an additional check with the PHP to make sure. Something like this:
Firstly cheers MstrBob for that, it's far simpler than wot i was gonna do. I'm now on to my next problem...
I've tried to create an article submission page to upload articles to the database. I've created the following code but can't work out why it doesn't work....<?php
//connect to the database
require_once ('../../db_connect.php');
//include the external function page
include ('functions.php');
if (isset($_POST['submit']))
{
//check that the title has been filled in
if (!empty($_POST['title']))
{
$title = escape_data($title);
}
else
{
$title = FALSE;
echo '<script type="text/javascript">alert("Please enter a title for the article");</script>';
}
//check the content has been entered
if (!empty($_POST['content']))
{
$content = escape_data($content);
}
else
{
$content = FALSE;
echo '<script type="text/javascript">alert("Please enter the main body of the article");</script>';
}
//check that the user has entered his/her name
if (!empty($_POST['author']))
{
$author = escape_data($author);
}
else
{
$author = FALSE;
echo '<script type="text/javascript">alert("Please enter your name");</script>';
}
//check a type has been selected
if (!empty($_POST['type']))
{
$type = escape_data($type);
}
else
{
$type = FALSE;
echo '<script type="text/javascript">alert("Please select the type of the article");</script>';
}
//continue if the form has been filled out correctly
if ($title && $content && $author && type)
{
//add the article to the database
$query = "INSERT INTO article (title, content, author, upload_date, type, images) VALUES ('$title', '$content', '$author', NOW(), '$type', '$images');"
$results = @mysql_query ($query);
//if the article was succesfully uploaded to the database
//say thank you, and stop the script from loading the form again
if ($results)
{
echo '<p class="cheers">That you for your submission $author.</p>'
exit();
}
//if did not apologies and show the mysql error message
//also email the site admin with information on the error
else
{
$error = mysql_error();
echo '<p class="error">The article was unable to be uploaded due to technical difficulties.
The site admin has been notified of the problem.<br />' . $error . '</p>';
//sets the strings to be used in the email
//$error_date = date('D d-m-Y', microtime());
//$admin_email = "admin@ugmfc.com";
//$error_subject = "Error uploading to database"
//$error_body = "An error has occured when" . $author . "was attemping to upload an article to the database. The following error code was produced: " . $error;
<div class="form_images">
<input type="button" value="Add Images" onclick="window.open('add_image.php', 'Add image', 'width=100, height=120, toolbar=0, menubar=0, status=0');" />
</div>
<div class="form_submit">
<input type="submit" value="Submit" />
</div>
</form>
</div>
AAF;
?>I'm only just learning php and have pieced the above code together from several books, and a little bit of my own ingenuity (I suspect the latter may be my undoing). There's a lot there, and i know it's a big ask, but if any body can shed some light on my errors, and help me progress to a php god (ok may be just a competent programmer) it would be much appreciated.
echo '<p class="cheers">That you for your submission $author.</p>';
and what's this:
$form = <<< AAF
....html here....
<<< AAF
???
Durbs
08-18-2004, 04:32 PM
Cheers buddy, you won't believe how long i sat staring at the screen - couldn't see for looking, as my granny would say.
The <<<AFF........AAF; is the heredoc syntax for creating multiple line strings, which continues to include subsequent lines into the string untill it sees the same label at the beginning of a line. The advantage of using it is you can then use ' & " without having to worry as much. Look here (http://uk2.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc) for more informaion. Again you are correct in that this is another element of the code that fails to execute, but i can't figure out why. As far as i can see i've implemented it correctly. I'll try just using the standard echo and see if that makes it run...
Durbs
MstrBob
08-18-2004, 04:44 PM
Oh, well, you learn something new everday. Right then, your issue is the indenting before the AAF;
It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system.
Bugger i really should learn to read the full article...
Durbs
08-18-2004, 06:40 PM
ok one last little question:
How do you read the date stored in a mySQL database (orignally stored using NOW() ) in php? I used:$query = "SELECT date
FROM database";
$results = mysql_query($query); When i read the date i used to date(); to format them but each date reads Thursday 01/01/1970 00:33 (33 mins from the unix epoch date, i think). Obviously the dates are not this but can't figure out wot i've done wrong. Is it that the timestamp used by mySQL would be stored something like 01-01-1970, and the date() format function works only with seconds from the ? I suppose if it is the answer is to use mkdate() to create a timestamp in seconds and upload that to a int and use that instead, correct? Any ideas/comments on this problem?
Durbs
08-18-2004, 07:01 PM
Don't worry, have just spotted the DATE_FORMAT function for mySQL, and will attempt to use that to format the date.
Durbs
Durbs
08-19-2004, 12:55 PM
Hi MstrBob i've been trying to use the code you suggested for the date selection. I'm trying to allow the users to browse summarised articles for selected dates that they specify. I've worked out what to do once the dates have been specified but can't get the form code you gave me to run. I've modified it to my needs, and as far as i can see it's all correct. Heres the code:$php_self = $_SERVER['PHP_SELF'];
$months = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
echo '</select><select name="year">';
for($y=2002;$y<=date('Y');$y++)
{
echo '<option value="' .$y. '">' .$y. '</option>';
}
echo '</select><input type="submit" name="Go" value="Go" /></form>';Is there any way of debugging php because at the moment i simply upload to the server and attempt to run it. If it works the page is displayed, if it doesn't it is not and neither are any error messages.
Cheers,
Durbs
Durbs
08-19-2004, 01:07 PM
Again just ignore me, i've sorted the problem. This happens to me EVERY chuffing time - I've sat here all day trying to work out wots wrong to no avail, as a last resort i post on here to get some help and as i hit the submit button ideas just start popping into my head. WHY couldn't they start popping sooner:confused:. NeHow the debugging questions still stands tho...
Cheers,
Durbs
MstrBob
08-19-2004, 05:54 PM
With PHP, if there's a synatx error, it tells you. However, if your logic is off, it can't tell you, because what's wrong, really? It's doing what you told it to do, and you correctly told it what to do. It's just that what you told the server to do isn't really what you want it to do. But how can the parser know that? :p
Erm, in terms of determining if something works or not, there's always:
or die("There was an error.")
I particularly find this handy when working with mysql. I do something like:
mysql_query($query) or die("<strong>Error #".mysql_errno().":</strong> ".mysql_error());
Basically it says, Do this. Of it fails, then do this. Luckily, mysql has error handling features that can provide me with much needed info on the issue. But other than that, there's really nothing generic I can think of...
webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved.