
July 23rd, 2004
12:05 PM
I'm a screenwriter. What am I doing here?
Status: Offline!
Multiple steps in one install script
Trying to make my own news script I downloaded a few for reference and noticed that although the setup process has multiple steps and a different form for each set, there was only one install.php (or whatever the install file is called.) For each step the previous form disappears and a new one appears in its place, and I tried to figure out the code behind this but couldn't do it. Does anyone know how this is done?
___________________

Dick Cheney Nude -- Sign the Petition!

July 23rd, 2004
02:07 PM
probably done using separate functions/chunks of code that are invoked based on which step the script is executing. this could be done with block if statements cordoning off different html tags/forms
there could be, say, 5 discrete html 'pages' embedded into one php file, but only one would be displaying at any given step, due to the others being ignored by php, and thus not sent to the browser. of course the pages would probably share titles, headers, footers, etc., but other than that, could logically be broken out into separate physical files to the same end
seems kind of messy imo. instead of a bunch of smaller, organized files you end up with one giant file that would tend to be harder to manage if the programmer isn't careful

July 23rd, 2004
02:48 PM
I'm a screenwriter. What am I doing here?
Status: Offline!
I've got something like this at the moment:
<?
if ($_GET["step"] == '') {
?>
<form action="index.php?step=2" method="post">
<table border="0" cellpadding="3">
<tr>
<td width="160">Database Name </td>
<td width="145"><input type="text" name="db_name" />
</td>
</tr>
<tr>
<td><p class="indent">Create this Database?</p></td>
<td><input type="checkbox" name="checkbox" value="checkbox" /></td>
</tr>
<tr>
<td>Database Username </td>
<td><input type="text" name="db_username" /></td>
</tr>
<tr>
<td>Database Password </td>
<td><input type="password" name="db_password" /></td>
</tr>
<tr>
<td>Database Server </td>
<td><input type="text" name="db_server" value="localhost" /></td>
</tr>
<tr>
<td>Table Prefix </td>
<td><input type="text" name="db_prefix" value="dranews_"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
<? }
if ($_GET["step"] == 2) {
?>
<form action="index.php?step=2" method="post">
<table border="0" cellpadding="3">
<tr>
<td width="160">Admin Username </td>
<td width="145"><input type="text" name="admin_user" value="Administrator" />
</td>
</tr>
<tr>
<td>Admin Password </td>
<td><input type="password" name="admin_pass" /></td>
</tr>
<tr>
<td>Confirm Password </td>
<td><input type="password" name="admin_conf" /></td>
</tr>
</table>
</form>
<? } ?>
It's an incomplete script (it really doesn't do anything) but the concept works so far. This is all rather new to me. I haven't really worked with forms or PHP or MySQL before at this level so it's going to be a bit difficult making a script like this. I usually learn stuff through osmosis and looking at other scripts for reference. Since it's just a news script it'd probably be okay to just download someone else's script but I'd rather make my own for some reason.
I couldn't get around using tables. I generally work with <div> and CSS but it was too difficult and used too much code to get things as neat as they are now so I'll just use tables. I don't know how splitting the { } in such a way doesn't generate any errors but it seems to get the job done so I'll go with it. The idea with the script so far is to connect to the database in Step 1 and create some tables, then start filling the tables with an admin username/pass (duh.)
___________________

Dick Cheney Nude -- Sign the Petition!
Last edited by O_ONanaki, July 23rd, 2004 02:57 PM (Edited 1 times)

July 23rd, 2004
03:14 PM
you can get around the nasty <? } ?> crap by using the alternate 'if' syntax -
<?php if(!array_key_exists('step',$_GET)): ?>
Installation Step <b>1</b> of 5
<hr>
<?php elseif($_GET['step'] == 2): ?>
Installation Step <b>2</b> of 5
<hr>
<?php endif; ?>
untested, but something like that. a bit cleaner

July 23rd, 2004
03:18 PM
I'm a screenwriter. What am I doing here?
Status: Offline!
I don't see how that would work but I'll try it.
___________________

Dick Cheney Nude -- Sign the Petition!

July 23rd, 2004
03:45 PM
whoa, wtf?
Status: Offline!
I would suggest
<?php
switch($_GET['step']){
case '1':
include 'stepone.php';
break;
case '2':
include 'steptwo.php';
break
default:
//user is at the beginning
}
?>
That's what I usually do. Keeps it clean.
___________________
Fomerly known as lasnaranjas. Holler.


July 23rd, 2004
04:45 PM

well it's just a different way of saying the same thing, that can clean up your code a bit. compare -
<?php if($blah == '1') { ?>
blah is equal to one
<?php } ?>
<?php elseif($blah == '2') { ?>
blah is equal to two
<?php } ?>
<?php else { ?>
blah is some other value
<?php } ?>
to
<?php if($blah == '1'): ?>
blah is equal to one
<?php elseif($blah == '2'): ?>
blah is equal to two
<?php else: ?>
blah is some other value
<?php endif; ?>
that syntax was presumably made for the functionality you're trying to do, just to keep you sane. it may not be obvious with my minimalist example, but try hiding '}' blocks inside hundreds of lines of html/php code, it can get confusing quickly .. especially if you need nested if's!
(to be fair I could have put the terminating }'s on the same line as the else's, though. but that is still messy compared to the 2nd version)
that said, I fully agree with lasnaranjas. for code maintainability, it would be wise to split out the steps into separate php scripts, as I mentioned earlier

July 24th, 2004
02:25 PM
I'm a screenwriter. What am I doing here?
Status: Offline!
Okay I did it my way, the complex and slightly sloppy way, and it was working fine. It seems now the trouble comes in passing along variables from if-to-if, the variables keep getting, well, erased. Someone said I have to declare them global and I did, and that didn't solve the problem. Bottom line, when I get to "Step 3" or if I return to the beginning of "Step 2" from the start, all the variables like $db_prefix and such no longer have a value assigned to them.
This is the fulle script. In fact you can actually run it yourself if you have a webserver and test everything out. But if you
a) Fail to enter a Admin password/email/username, get the error caused by doing such, and try to return to Step 2 to enter such info, you will get the error that you are unable to connect to the database... Which you should be able to.
b) Reach Step 3, which is incomplete on purpose, you will discover that variables like $db_prefix no longer have a value assigned, which they should, I thought, since I declared them global variables and they are given values in Step 2.
Hope this all makes sense.
<? error_reporting(E_ERROR) ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Dragoner News Installation</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="container">
<div id="title">Dragoner News Installation</div>
<div id="form">
<?
global $db_name,$db_username,$db_prefix,$db_password,$db_server;
$step = $_GET["step"];
if (!$_GET["step"]) {
?>
<form action="index.php?step=2" method="post">
<table border="0" cellpadding="3">
<tr>
<td width="160">Database Name </td>
<td width="145"><input type="text" name="db_name" />
</td>
</tr>
<tr>
<td><p class="indent">Create this Database?</p></td>
<td><input type="checkbox" name="create" value="yes" /></td>
</tr>
<tr>
<td>Database Username </td>
<td><input type="text" name="db_username" /></td>
</tr>
<tr>
<td>Database Password </td>
<td><input type="password" name="db_password" /></td>
</tr>
<tr>
<td>Database Server </td>
<td><input type="text" name="db_server" value="localhost" /></td>
</tr>
<tr>
<td>Table Prefix</td>
<td><input type="text" name="db_prefix" value="dranews_"/></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /></td>
</tr>
</table>
</form>
<? }
if ($step == 2) {
$db_name = $_POST["db_name"];
$create = $_POST["create"];
$db_username = $_POST["db_username"];
$db_password = $_POST["db_password"];
$db_server = $_POST["db_server"];
$db_prefix = $_POST["db_prefix"];
echo $db_prefix;
}
if ($step == 2 OR $step == 2.5) {
$connection = mysql_connect($db_server,$db_username,$db_password);
if (!$connection) {
die("<form action=\"index.php\"><table>
<tr>
<td>Could not connect to database server.</td>
</tr>
</table>
<input type=\"submit\" value=\"Back\" />
</form>");
}
if ($create == "yes") {
$db_selected = mysql_select_db($db_name);
if ($db_selected) {
die("<form action=\"index.php\"><table>
<tr>
<td>Could not create database.</td>
</tr>
<tr>
<td>Possible reasons:</td>
</tr>
<tr>
<td>
<ul>
<li>The database already exists.</li>
</ul>
</td>
</tr>
</table>
<input type=\"submit\" value=\"Back\" />
</form>");
}
$createdb = mysql_create_db($db_name);
if (!$createdb) {
die("<form action=\"index.php\"><table>
<tr>
<td>Could not create database.</td>
</tr>
<tr>
<td>Possible reasons:</td>
</tr>
<tr>
<td>
<ul>
<li>The database already exists.</li>
<li>You do not have permission to create databases.</li>
</ul>
</td>
</tr>
</table>
<input type=\"submit\" value=\"Back\" />
</form>");
}
}
$db_selected = mysql_select_db($db_name);
if (!$db_selected) {
die("<form action=\"index.php\"><table>
<tr>
<td>Could not connect to the database.</td>
</tr>
<tr>
<td>Possible reasons:</td>
</tr>
<tr>
<td>
<ul>
<li>The database does not exist.</li>
<li>You misspelled the database name.</li>
<li>You do not have permission to access the database.</li>
</ul>
</td>
</tr>
</table>
<input type=\"submit\" value=\"Back\" />
</form>");
}
?>
<form action="index.php?step=3" method="post">
<table border="0" cellpadding="3">
<tr>
<td width="160">Admin Username </td>
<td width="145"><input type="text" name="admin_user" value="Administrator" />
</td>
</tr>
<tr>
<td>Admin E-Mail </td>
<td><input type="text" name="admin_email" /></td>
</tr>
<tr>
<td>Admin Password </td>
<td><input type="password" name="admin_pass" /></td>
</tr>
<tr>
<td>Confirm Password </td>
<td><input type="password" name="admin_conf" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</form>
<? }
if ($step == 3) {
echo $db_prefix;
$admin_user = $_POST["admin_user"];
$admin_email = $_POST["admin_email"];
$admin_pass = $_POST["admin_pass"];
$admin_conf = $_POST["admin_conf"];
if (!$admin_user OR !$admin_pass OR !$admin_email) {
die("<form action=\"index.php?step=2.5\" method=\"post\">
<table>
<tr>
<td>Must fill in all fields.</td>
</tr>
</table>
<input type=\"submit\" value=\"Back\" />
</form>");
}
elseif ($admin_pass != $admin_conf) {
die("<form action=\"index.php?step=2.5\">
<table>
<tr>
<td>Passwords don't match.</td>
</tr>
</table>
<input type=\"submit\" value=\"Back\" />
</form>");
}
if (!$db_prefix) {
echo("Prefix doesn't exist.");
}
}
?>
</div>
</div>
</body>
</html>
A mere 200 lines. If you skip the junk and concentrate on why the variables aren't remaining it may be easier to make sense out of.
___________________

Dick Cheney Nude -- Sign the Petition!

July 25th, 2004
01:02 AM
yeah, since it's stateless, it has no way to remember your variables. you have to manually send them to all the steps in which you want to access them
to do this just pass them via POST every step. all you have to do is create hidden fields with the same names as the text input fields under the step 'if' branch in question
you could use sessions or even cookies if you really wanted to, but the first is what I'd do

July 25th, 2004
03:23 AM
I'm a screenwriter. What am I doing here?
Status: Offline!