Jump to content

ScorchPipe

New Members
  • Posts

    8
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

ScorchPipe's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. A) Yeah I had a problem with that. But I were planning of solving it later =) A/B/C) Can you give me some sample code to work with. I have no idea how to write this. I'm not a coder at all normally.
  2. Hi! I'm working on a projekt that I need help with. I'm completely stuck. The code will 1. Get rows from a mysql-table. 2. Print a checkbox for each row. 3. In the table there's a column named "enabled" which is either 0 or 1. If enabled=1 the checkbox for that row will be checked and vice versa. 4. You should be able to check AND uncheck boxes however you want, hit submit and all rows will get updated with either enabled=0 or enabled=1. The first 3 works, but I'm still missing number 4. No idea on how to do this. Can anyone explain how I can write the missing code? <?php # db-information to variables $host = "localhost"; $username = "root"; $userpass = "mypassword"; $dbname = "drift"; # Connect to db $dbconnect = mysql_connect($host,$username,$userpass); if (!$dbconnect) { die("Could not connect to database. " . mysql_error()); } # Choose db $dbselect = mysql_select_db($dbname,$dbconnect); if (!$dbselect) { die("Could not select database. " . mysql_error()); } ########### Get the rows and print them like checkboxes echo "Välj platser:<br />"; echo "<form action=\"test.php\" method=\"post\">"; $query = mysql_query("SELECT * FROM drifttable"); while($result = mysql_fetch_assoc($query)) { if ($result[enabled] == 1) { echo "<input type=\"checkbox\" name=\"places[]\" value=\"$result[id]\" checked=\"checked\">$result[descr]<br />"; } elseif ($result[enabled] == 0) { echo "<input type=\"checkbox\" name=\"places[]\" value=\"$result[id]\">$result[descr]<br />"; } else { exit ("Couldn't get 0 or 1 from 'enabled' in db"); } } echo "<input type=\"submit\" />"; echo "</form><br />"; ########### Update the db with new values ########### Closes the MySQL connection mysql_close(dbconnect); ?>
  3. Thanks for the help! I should've figured that one out =) I'm doing it like this because it was the best method I could come up with without putting HTML code in PHP or vice versa
  4. Hello I'm making a guestbook in PHP and I'm having trouble displaying the posts. The user fills a form and sends it, it goes into the database (mysql) and I can then fetch the data. So far so good. I'm then going to use HTML to display the posts to the user. It's code looking like this: <p> <br /> ID: ---replaceid--- <br /><br /> Time: ---replacetime--- <br /> From: <a href="---replacehomepage---">---replacefrom---</a> <br /> Email: ---replacemail--- <br /> <br /> Comment: ---replacecomment--- <br /><hr> </p> I want to use str_replace in PHP to replace the values in HTML... like this: $numquery = mysql_query("SELECT * FROM guestbook", $dbconnect); $num = mysql_num_rows($numquery); $i=1; while ($i < $num) { header('Content-type: text/html'); $html = file_get_contents("test.html"); $query = mysql_query("SELECT * FROM guestbook WHERE id='$i'", $dbconnect); $rows = mysql_fetch_row($query); echo str_replace('---replaceid---', nl2br($rows[0]), $html); echo str_replace('---replacetime---', nl2br($rows[1]), $html); echo str_replace('---replacefrom---', nl2br($rows[2]), $html); echo str_replace('---replacemail---', nl2br($rows[3]), $html); echo str_replace('---replacehomepage---', nl2br($rows[4]), $html); echo str_replace('---replacecomment---', nl2br($rows[5]), $html); $i++; } But this only replaces ---replaceid---, leaves the rest and outputs it. Then, only ---replacetime--- is replaced and so on. I hope you understand. It means that 1 guestbook entry is displayed like 6, with only 1 string replaced at each one. What do you think I should do? Worth saying is that I'm not allowed to mix PHP-code with HTML-code
  5. That bad, huh.... Well coding is not really my field expertise (more into servers and stuff). I had empty space to fill and I figured a course about serverside php could be useful sometime =)
  6. It worked! Really thanks! Haha funny thing about the eval-line. I'm taking this PHP course online from a university and the teacher acctually recommended using that line for another assignment which is pretty much the same as this one, but with only 1 line. Thats also why the comments are a bit off. They dont belong to this code
  7. Hmm... nothing happened by adding just that. Is there something else I need to do?
  8. Hi! I'm new to PHP so bear with me =) I have an assignment where I have to use PHP to get all enviromental variables and output them as HTML in the web browser. But I have to do this without mixing HTML and PHP in the same file. Getting the enviromental variables is not a problem and I can also output them as HTML. But I can't get them to be on one line per variable. Now I get: name: valuename: valuename: valuename: valuename: value and so on but I want it to be like: name: value name: value name: value Here is my HTML code: <?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> 3.1 </title> </head> <body> <p> ---insert--- </p> </body> </html> And here is my PHP code: <?php # Set text/plain instead of text/html header('Content-type: text/html'); $html = file_get_contents("page.html"); # Function that gets and prints all enviroment variables function loop(){ foreach ($_SERVER as $name=>$value ) { echo "$name: $value\n"; } } # Prints the contents of page.html and replaces ---insert--- with number of hits eval("print \"" . addcslashes(preg_replace("/(---(.+?)---)/" . loop() . $html), '"') . "\";"); ?> Because of header('Content-type: text/html'); \n in the foreach loop doesn't work. It works perfect if I replace it with <br /> but thats not allowed. It also works if I set text/plain instead of text/html but to output it as HTML is a requirement. Any idea how I can solve this? Anything is appreciated as long as it doesn't break any rules.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.