Please help me with creating admin page, SQL UPDATE problem.

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
User avatar
Marnboorapa
Registered User
Posts: 84
Joined: Sat Jan 08, 2005 3:19 pm
Location: Thailand
Contact:

Please help me with creating admin page, SQL UPDATE problem.

Post by Marnboorapa »

I'm trying create a new litle mod for use with my site, but I have a problem with ACP page, I want to make it update table for var to "1" or "0"

for the mod I have insert 1 field to categories table which named "cat_article",
and 1 field to forum table which named "forum_article",

and I would like to create a admin page to array categories and forums list, then have a option to set "Yes" or "No", (Yes = 1), (No = 0) for 'cat_artilce' and 'forum_article'.

Screen shot
Image

I understand how to create new admin page ( admin_xxx.php + admin_xxx_body.tpl + new lang vars ), but with php script I can't, some one please help me.

User avatar
CyberGeek828
Registered User
Posts: 94
Joined: Thu Oct 12, 2006 1:21 am

Post by CyberGeek828 »

Add the following to the top of the page to make it appear in the admin section. Also, amke sure you put it in root/admin (and i say this out of past experiences because I forgot and was told off for it :oops: )

Code: Select all

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['General']['Configuration'] = $file;
	return;
}
I'd help with the rest but I'm not advanced enough. Good Luck!
Scott

A.I. BOT
Registered User
Posts: 1848
Joined: Thu Apr 17, 2003 11:43 pm
Location: Newfoundland, Canada
Contact:

Post by A.I. BOT »

I am not exactly sure of your problem. So I will try my best ...
To display the checkmark for yes/no per forum/category you will need to do something like this (havent tested any of this, couldbe totally wrong):

Code: Select all

// Get the categories from the database.
$sql	= 'SELECT *
	FROM ' . CATEGORIES_TABLE . '
	ORDER BY cat_order';
$result	= $db->sql_result ( $sql );

// This will loop though the categories ...
for ( $i = 0; $i < $db->sql_numrows ( $result ); $i++ )
{
	$row	= $db->sql_fetchrow ( $result );

	// Now we do the checkbox/checkmark stuff ...
	// ( condition ) ? true : false ...
	// so if cat_article is 1, then it is true, else false.
	$template->assign_block_vars ( 'block_cats', array (
		'S_CATNAME' => $row[ 'cat_name' ],
		'S_CATID' => $row[ 'cat_id' ],
		'S_CHECKMARK1' => ( $row[ 'cat_article' ] == 1 ) ? 'checked="checked"' : '', # for "Yes"
		'S_CHECKMARK2' => ( $row[ 'cat_article' ] == 0 ) ? 'checked="checked" : '' ) # for "No"
	);

	// Get the forums from the database.
	$sql_2		= 'SELECT *
		FROM ' . FORUMS_TABLE . '
		WHERE cat_id = ' . $row[ 'cat_id' ] . '
		ORDER BY cat_id, forum_order';
	$result_2	= $db->sql_result ( $sql_2 );

	// This will loop though the forums ...
	for ( $x = 0; $x < $db->sql_numrows ( $result_2 ); $x++ )
	{
		$row_2	= $db->sql_fetchrow ( $result_2 );

		// Now we do the checkbox/checkmark stuff ...
		// ( condition ) ? true : false ...
		// so if forum_article is 1, then it is true, else false.
		$template->assign_block_vars ( 'block_cats.block_forums', array (
			'S_FORUMNAME' => $row_2[ 'forum_name' ],
			'S_FORUMID' => $row_2[ 'forum_id' ],
			'S_CHECKMARK1' => ( $row_2[ 'forum_article' ] == 1 ) ? 'checked="checked"' : '', # for "Yes"
			'S_CHECKMARK2' => ( $row_2[ 'forum_article' ] == 0 ) ? 'checked="checked" : '' ) # for "No"
		);
	}
}
Somewhere else where you handle your submit put:

Code: Select all

// Loop though each category on the form in the ACP.
$i = 0;
foreach ( $HTTP_POST_VARS[ 'cat' ] AS $cat_id )
{
	// Yes or No ?
	$yes_no	= ( $HTTP_POST_VARS[ 'cat_yes' ][ $i ] == 1 ) ? 1 : 0;

	// Update them in the databse.
	$sql	= 'UPDATE ' . CATEGORIES_TABLE . ' SET cat_article = ' . $yes_no . ' WHERE cat_id = ' . $cat_id;
	$result	= $db->sql_result ( $sql );
	$i++;
}

// Loop though each forum on the form in the ACP.
$x = 0;
foreach ( $HTTP_POST_VARS[ 'forum' ] AS $forum_id )
{
	// Yes or No ?
	$yes_no	= ( $HTTP_POST_VARS[ 'forum_yes' ][ $x ] == 1 ) ? 1 : 0;

	// Update them in the databse.
	$sql	= 'UPDATE ' . FORUMS_TABLE . ' SET forum_article = ' . $yes_no . ' WHERE forum_id = ' . $forum_id;
	$result	= $db->sql_result ( $sql );
	$x++;
}
Now in the TPL file put something like:

Code: Select all

<form action="[b]FIX THIS[/b]" method="post">
<!-- BEGIN block_cats -->
	{block_cats.S_CATNAME} Yes: <input type="checkbox" name="cat_yes[]" value="1" {block_cats.S_CHECKMARK1} /> || No: <input type="checkbox" name="cat_no[]" value="0" {block_cats.S_CHECKMARK2} /> <input type="hidden" name="cat[]" value="{block_cats.S_CATID}" /><br />
	<!-- BEGIN block_forums -->
		- {block_cats.block_forums.S_FORUMNAME} Yes: <input type="checkbox" name="forum_yes[]" value="1" {block_cats.block_forums.S_CHECKMARK1} /> || No: <input type="checkbox" name="forum_no[]" value="0" {block_cats.block_forums.S_CHECKMARK2} /> <input type="hidden" name="forum[]" value="{block_cats.block_forums.S_FORUMID}" /><br />
	<!-- END block_forums -->
<!-- END block_cats -->
<input type="submit" name="submit" value="Submit" />
</form>
I hope that help you or aleast takes you in the right direction if im wrong.
Thanks.

User avatar
Ptirhiik
Registered User
Posts: 7411
Joined: Mon Jan 06, 2003 10:36 pm
Contact:

Post by Ptirhiik »

Is "No" receiving a different process than not "No" and not "Yes" ? If no, you don't need the "No" column.

> A.I. BOT: regarding the ids reading: it is always a bad idea to trust the ids sent by the form without checking it against the expected ids. Though it is not a big deal in this very case - as long as you sanatize your readed ids with an intval(), what you have forgot in your given code :) - at worst, the update query fails, I found a lot of mods that use the way you have quoted, and allow so to act on unauthorized rows with returning not-expected ids in the $_POST buffer.

User avatar
Marnboorapa
Registered User
Posts: 84
Joined: Sat Jan 08, 2005 3:19 pm
Location: Thailand
Contact:

Post by Marnboorapa »

My mod is look like easy article system, which use forum table ( don't need to create new table ), the idea is look like this

if forums and cats seted to " Yes " it will have some effect like this

1. not show on index.php but show on article.php
2. jumpbox will not show forums and cats which seted"Yes"

new on option for search.php, and jumpbox

search.php will show article_view.php instead of viewtopic.php

I create new jumpbox function named " jumbox_article" for use at article section

and mand of modified scripts for this article mod such as new layouts and article quick reply.

OK every things about the article section is done, and Now I will create ACP page to control forum and cat.

User avatar
Ptirhiik
Registered User
Posts: 7411
Joined: Mon Jan 06, 2003 10:36 pm
Contact:

Post by Ptirhiik »

So you don't need a "No" column indeed. I'm not even sure you need a forum checkbox : what if you have a category designed for news, and a forum within not checked ? Won't be your jumpbox a bit weird ?

User avatar
Marnboorapa
Registered User
Posts: 84
Joined: Sat Jan 08, 2005 3:19 pm
Location: Thailand
Contact:

Post by Marnboorapa »

No, new forum will always set to "No" because default is = "0", with jumpbox I just create a new function and make it select forum new field if = "1" jumpbox article will show it, but if default "0" it'll not show.

User avatar
Marnboorapa
Registered User
Posts: 84
Joined: Sat Jan 08, 2005 3:19 pm
Location: Thailand
Contact:

Post by Marnboorapa »

OK this code for array cat and forums list

Code: Select all

<?php
/***************************************************************************
 *                             article_admin_body.php
 *                            -------------------
 *   begin                : Thursday, Jul 12, 2001
 *   copyright            : (C) 2001 The phpBB Group
 *   email                : support@phpbb.com
 ***************************************************************************/
/***************************************************************************
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 ***************************************************************************/

define('IN_PHPBB', 1);

if( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['Content']['Articles'] = $file;
	return;
}

//
// Load default header
//
$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);
include($phpbb_root_path . 'includes/functions_admin.'.$phpEx);

//
// Output the authorisation details
//

$sql = "SELECT cat_id, cat_title, cat_order
	FROM " . CATEGORIES_TABLE . "
	ORDER BY cat_order";
if( !$q_categories = $db->sql_query($sql) )
{
	message_die(GENERAL_ERROR, "Could not query categories list", "", __LINE__, __FILE__, $sql);
}
if( $total_categories = $db->sql_numrows($q_categories) )
{
	$category_rows = $db->sql_fetchrowset($q_categories);
	$sql = "SELECT *
		FROM " . FORUMS_TABLE . "
		ORDER BY cat_id, forum_order";
	if(!$q_forums = $db->sql_query($sql))
	{
		message_die(GENERAL_ERROR, "Could not query forums information", "", __LINE__, __FILE__, $sql);
	}
	if( $total_forums = $db->sql_numrows($q_forums) )
	{
		$forum_rows = $db->sql_fetchrowset($q_forums);
	}

	//
	// Okay, let's build the index
	//
	$gen_cat = array();

	for($i = 0; $i < $total_categories; $i++)
	{
		$cat_id = $category_rows[$i]['cat_id'];
		$template->assign_block_vars("catrow", array( 
			'CAT_ID' => $cat_id,
			'CAT_DESC' => $category_rows[$i]['cat_title'],
			'U_VIEWCAT' => append_sid($phpbb_root_path."index.$phpEx?" . POST_CAT_URL . "=$cat_id"))
		);
		for($j = 0; $j < $total_forums; $j++)
		{
			$forum_id = $forum_rows[$j]['forum_id'];
			
			if ($forum_rows[$j]['cat_id'] == $cat_id)
			{
				$template->assign_block_vars("catrow.forumrow",	array(
					'FORUM_NAME' => $forum_rows[$j]['forum_name'],
					'ROW_COLOR' => $row_color,
					'U_VIEWFORUM' => append_sid($phpbb_root_path."viewforum.$phpEx?" . POST_FORUM_URL . "=$forum_id"))
				);
			}		
		}
	} 
}

$template->set_filenames(array(
	'body' => 'admin/article_admin_body.tpl'
	)
);
$template->assign_vars(array(
	'S_FORUM_ACTION' => append_sid("article_admin_body.$phpEx"),
	'L_FORUM_TITLE' => $lang['Forum_admin'])
);

$template->pparse('body');

include('./page_footer_admin.'.$phpEx);

?>

but I'm realy not understand with form system to sent value for update SQL , some one please suggest me.

User avatar
Brf
Support Team Member
Support Team Member
Posts: 51997
Joined: Tue May 10, 2005 7:47 pm
Location: {postrow.POSTER_FROM}
Contact:

Post by Brf »

When you click your submit button on the form, it will set $HTTP_POST_VARS['submit'] = 'Submit. You check for that in your script and then pull the rest of the new values from the form and update your database.

Like this:

Code: Select all

(read values from database)

$submit = isset($HTTP_POST_VARS['submit']); 
if ($submit) {
(read values from form)
}

(load template for display)

if ( $submit )
{
(update database)
}

User avatar
Ptirhiik
Registered User
Posts: 7411
Joined: Mon Jan 06, 2003 10:36 pm
Contact:

Post by Ptirhiik »

Marnboorapa wrote: No, new forum will always set to "No" because default is = "0", with jumpbox I just create a new function and make it select forum new field if = "1" jumpbox article will show it, but if default "0" it'll not show.
Yes, so if you check a category for news, and have a forum within which is not checked for news, ie a new created one, you won't show the forum in your regular jumpbox (the one displayed any and every where) though it is not a news forum, and the topics within this forum won't appear in your news (as it is not a news forum). At contrary, a categories not checked with all forums checked will appears empty, or won't appears at all (depending the way you have handled it). Perhaps a desired effect, but this sounds a bit weird to me ;): either you trust the forum status, then you don't need the categories status, either you trust the categories status, and so don't need the forums status (or it will have a different meaning, ie a draft news forum).


Another organization for your algorithm, which can apply to any transaction:

Code: Select all

- init: read data from database (always, you need them to check the ids from the form),
if ( submit )
{
	$error = false;
	- check form values (if any), set $error if appropriate
	- if not $error: update database, send confirmation message through message_die()
}
- send error messages and display

User avatar
Marnboorapa
Registered User
Posts: 84
Joined: Sat Jan 08, 2005 3:19 pm
Location: Thailand
Contact:

Post by Marnboorapa »

OK now every thing is done. I already added an ACP page to control my mod.

Post Reply

Return to “[2.0.x] MOD Writers Discussion”