Web designing newb. Creating a PHP forum from scratch!

Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
That didn't work Matt. Sorry I should have included more php, I did indeed have a form, but the method was set to post instead of get? Not that it made a difference, is there any reason in particular to my method?

Talbs, care to elaborate?
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
Right, it's not even echo'ing anything after if result is successful.

Code:
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>

//A fair amount of html stuff . . 

<?php
while($rows=mysql_fetch_array($result))
{ // Start looping table row
?>

<tr>
<td>&nbsp;</td>
<td align="center" bgcolor="#000000"><? echo $rows['id']; ?></a><BR></td>
<td align="center" bgcolor="#000000"><? echo $rows['myusername']; ?></td>
<td align="center" bgcolor="#000000"><? echo $rows['email']; ?></td>
<td align="center" bgcolor="#000000"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></td>
<tr>
</tr>
<?php
}
?>
<table align="center" class="t8">
<tr>
<td align="right" ><input class="button" name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</table>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful echo WTF ffs etc
if($result){
echo "wtf";
}
}
mysql_close();
?>

</form>
</table>
 
Soldato
Joined
6 Feb 2004
Posts
20,599
Location
England
Code:
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

where are $delete and $checkbox coming from??

anyway, i'd use a foreach loop instead. it looks nicer. :p

Code:
<?php
if($_POST['delete']) {
    $success = 0;
    $error = 0;
    foreach($_POST['checkbox'] as $key => $value) {
        $result = mysql_query("DELETE FROM $tbl_name where id = '$value'");
        if($result) {
            $success++;
        } else {
            $error++;
        }
         //you could replace that if/else statement with this one line of code....
         //  ($result) ? $success++ : $error++;
         //but i didn't want to confuse things too much. :p
    }
    echo $success. ' records deleted successfully. There were ' . $error . ' errors.';
}

also place this code before you run the query that gets the results for the table display. i think the reason for that is obvious. :p

also, i'm assuming you have to be logged on as admin to view this page, so i haven't bothered with sanitising input. :)
 
Last edited:
Soldato
Joined
17 Sep 2005
Posts
2,983
Location
Everywhere
Talbs, care to elaborate?

Sure.

Basically it adds all the userid's into an array and then your SQL Query will update/delete all of those ID's in the array. So..

Your "Delete?" checkbox will be...
Code:
<input type="checkbox" name="selected_userid[]" value="<?=$data['userid'];?>">

You can then do a function to action the delete/update query.

Code:
function delete_users($userid) {

mysql_query("DELETE FROM user WHERE userid='$userid'");
echo mysql_error();

echo "User(s) Deleted";

// do redirect code

}

Then you need to use "array_walk" to run the function on each of the selected items. So..

Code:
array_walk($_POST['selected_userid'], 'delete_users');

"delete_users" being the name of the function and the "selected_userid" contains all of the userid's selected from the checkbox.

So put all together as an example.

Code:
if ($delete) {

function delete_users($userid) {

mysql_query("DELETE FROM user WHERE userid='$userid'");
echo mysql_error();

echo "User(s) Deleted";

// do redirect code

}

array_walk($_POST['selected_userid'], 'delete_users');

} else {

// do form with checkbox

?>
<form action="<?=$php_self;?>" method="post">
BLAH BLAH
<input type="checkbox" name="selected_userid[]" value="<?=$data['userid'];?>">

<input type="submit" name="delete" value="Delete User(s)">

<?
}
?>

Im sure people will pick holes with that but its a basic example.
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
snip

1) also place this code before you run the query that gets the results for the table display. i think the reason for that is obvious. :p

2) also, i'm assuming you have to be logged on as admin to view this page, so i haven't bothered with sanitising input. :)
That method worked a treat, thanks :)


A couple of questions:

1) It seems to be working using the same structure as above. When I budge things up, it gets upset. Any reason in particular for your reason? Seems to be working fine at the moment.

2) Unfortunately, I'm yet to have admin features added. I have the following plan of attempting to do so:
i) Create an extra column in the members db which differentiates admins from normal users (all users default to normal users)​
ii) Have a statement on every page like sessions, to check if user is an admin. If so, display the extra powers.​


What do you think?

Thank you for your input talbs, nice to gain more knowledge :)
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
is it not blindingly obvious? :confused:

you must process the deletion of users before running the query to display all users, otherwise you're going to be displaying users that are no longer in the database by time the page has finished loading.

Isn't it okay the way it is?

Code:
Connect to DB

Select * from tbl_name

html

Start looping table row

html

Finish looping table row

Functional buttons

Run the for loop

It works fine this way, as I see it the following way:

Code:
Retrieve

Delete

Refresh
That does not display any deleted members, all my 20 pages of php are done in a similar fashion.
 
Soldato
Joined
6 Feb 2004
Posts
20,599
Location
England
It works fine this way, as I see it the following way:

Code:
Retrieve

Delete

Refresh
That does not display any deleted members, all my 20 pages of php are done in a similar fashion.

so you're refreshing the page after the delete. that explains why it works but to me that is a completely backward way of doing things.

what you're really doing is this

Code:
retrieve (for no reason whatsoever)

delete

refresh

retrieve again (this is why things are displayed ok)

whereas my method is

Code:
delete

retrieve
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
Thanks Marc, I'll look into that if I get time.

I've managed to book holidays for this weekend. Deadline is for Monday and I have a lot of work lined up for me! Wish me luck :)
 
Soldato
Joined
10 Dec 2003
Posts
6,348
Regarding the colours. I can't imagine reading a forum with that theme for very long.

Try using ye olde standard white background, with smoother pastel colours that blend well. It'll be much easier on the eyes. You could maybe add a faint gradient too, in the direction of the content. It's all psychological. :p :)

Just my opinion.
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
Thanks for the input . . I've gone too far into the design, there's no chance of a revamp, it's only for an assignment, once it's marked it shall be archived for reference :)

A quick one . . Once a user logs in and comes back to the main page, how would I go about customising the page? For example, when a guest visits the forum they can select the logout option which is merely pointless. However, I'd like to tailor it so that guests can only see limited options.

I've also managed to sort out registration confirmation emails! :)

Now the following to go:
1) Avatars
2) Admin/Normal users
3) Editing posts
4) Quoting posts​

I haven't got time for validation as we were hinted they wouldn't check but recommended we should do so.
 
Last edited:
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
Bah, not long to go and not doing too well.

I've created a control panel which allows a user to insert a url for an avatar, however I've now become confused :\

Code:
<!-- Connect shizzle -->

$myusername=$_SESSION['myusername'];
$avatar=$_POST['avatar'];

$sql="INSERT INTO $tbl_name(myusername, avatar)VALUES('$myusername', '$avatar')";
$result=mysql_query($sql);

<!--My table/form etc . .-->
That just screams wrong to me, How would I make it read the appropriate username and then insert a url?
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
Awesome it's now storing :)

However, what's the best way to tackle this?

I have 3 tables . . create thread - create reply - members

My view thread displays the original post and then replies after it. Should I just echo the avatar corresponding to the member ID?

Thanks once again Marc :D
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
I've got 6 results tables now in my view thread. Should it be along these lines?:

Code:
<?php
$tbl_name2="members"; // Switch to table "members"

$sql2="SELECT * FROM $tbl_name2 WHERE avatar='$avatar'";
$result2=mysql_query($sql2);

?>

Code:
<td>&nbsp;<img src="$avatar['avatar]" width="62" height="80" alt="Avatar" border="0" />&nbsp;</a></td>
 
Soldato
Joined
6 Feb 2004
Posts
20,599
Location
England
i assume you've already got thread/post display working and it shows the username next to each post. adding the avatar in along with that should be a trivial matter??
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
It 'should' be however, my layout is posing a large problem.

Code:
Connect to DB

Retrieve original thread details 

Display header table and all usual html stuff

*Connect to table members, to retrieve avatar info* ~This is the one where everything messes up~

HTML/CSS display original post

Connect to table 'create reply'

Retrieve reply details if they exist

Retrieve data and add views

Then add replies if done

Quick reply box

I've learnt from my errors, however now time is running out so I have to make do with what I have now :\
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,867
Location
UK
Had to shrink it a fair amount, here's what it's like:

49267500tf7.jpg
 
Soldato
Joined
10 Dec 2003
Posts
6,348
Thanks for the input . . I've gone too far into the design, there's no chance of a revamp, it's only for an assignment, once it's marked it shall be archived for reference :)

That's fair enough then. Next time though, you may want to keep an ordered CSS file. Maybe even use different stylesheets for different elements of the design. This will make it easier to change the appearance of the site, and a hell of a lot quicker. :)

Good luck with. Been following with interest. :)
 
Back
Top Bottom