Jump to content

Help with swich...


feri_soft

Recommended Posts

I saw one very good tutorial for pages here but...
thats my original sorce code for viewing tutorials and their categories:
[code]<?php
include 'connect.php';



//get the id, put it into a variable, cast to an INT

//(for security purposes)
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 1;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
$id = (int)$_GET['id'];

$query = mysql_query("SELECT * FROM tutorials WHERE cat_id = '1' AND is_validated = '1' ORDER by id DESC LIMIT $from, $max_results ") or die(mysql_error());

//if no results, show that there are no tutorials

//for that category

if(mysql_num_rows($query) == 0)

{

echo "No tutorials for this category yet!";

}

//else, there is..show em

else

{

echo "<h1>Tutorials</h1>";

//loop through the tutorials

//show all tutorials

echo "<table border='0' cellpadding='0' cellspacing='0' width='500'>";

while($row = mysql_fetch_array($query))

{

echo "

<tr>

<td>Title:</td>

<td><b>$row[title]</b></td>

</tr>

<tr>

<td>Date Submitted:</td>

<td><b>$row[date_submitted]</b></td>

</tr>

<tr>

<td>Views:</td>

<td>$row[views]</td>

</tr>

<tr>

<td>Short Description:</td>

<td>$row[short_description]</td>

</tr>

<tr>

<td>Submitter:</td>

<td><a href='http://84.252.50.57/com/index.php?showuser=$row[poster_id]' target='_blank'>$row[submitter]</a></td>

</tr>

<tr>

<td colspan='2'><center><b><a href='$self?action=viewtutorial&id=$row[id]'>View</a></b></center></td>

</tr>

<tr>

<td colspan='2'><hr /></td>

</tr>

";

}

echo "</table>";

}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tutorials"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Select a Page<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Previous</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next>></a>";
}
echo "</center>";

//------------------------------------------

//this case gets the given [ID]

//action=viewtutorial&id=[ID]

//and gets that tutorial ID from the database

//and displays it!

//------------------------------------------


//if there is an ID given..

if($_GET['id'])

{

//set $id to the URL id, cast to an INT

//for security purposes

$id = (int)$_GET['id'];

//query the database

$query = mysql_query("SELECT * FROM tutorials WHERE id = '$id' LIMIT 1") or die (mysql_error());

//if no rows returned...

if(mysql_num_rows($query) == 0)

{

echo "That ID is not in the database!";

}

//else, show it!

else

{

//update the views for this tutorial!

$update_views = mysql_query("UPDATE tutorials SET views = views + 1 WHERE id = '$id'") or die(mysql_error());

//loop through the database

while($row = mysql_fetch_array($query))

{
print "<form action='ratearticle.php' method='post'>";
         print "<input type='hidden' name='id' value='$id '>";
         print "<Select name='rankselect'>";
         print "<option>1</option>";
         print "<option>2</option>";
         print "<option>3</option>";
         print "<option>4</option>";
         print "<option>5</option>";
         print "</select><br>";
         print "<input type='submit' name='submit' value='Vote'></form><br><br>";
echo "

<table border='0' cellpadding='0' cellspacing='0' width='500' style='border: 1px solid black; padding: 3px;'>

<tr>

<td colspan='2'>Tutorial: <b>$row[title]</b></td>

</tr>

<tr>

<td colspan='2' style='border: 1px solid black;'><center><b>Tutorial</b></center><br />$row[text]</td>

</tr>

<tr>

";

//----------------------------

//this part of the code

//checks to see if the submitter

//wants an email left for support

//----------------------------

if($row['show_email'] == 1)

{

echo "

<td colspan='2'>This tutorial was submitted by <b>$row[submitter]</b> with an email left for support questions. Contact this person <a href='mailto:$row[email]'>here</a></td>

";

}

echo "

</tr>

<tr>

<td><hr /></td>

</tr>

";

}

//--------------------------------

//this is where we loop through the

//comments table to show all the

//comments for this tutorial

//--------------------------------

$comments = mysql_query("SELECT * FROM tutorials_comments WHERE tut_id = '$id' ORDER BY id DESC") or die (mysql_error());

//if there are no comments..

if(mysql_num_rows($comments) == 0)

{

echo "

<tr>

<td colspan='2'>No comments for this tutorial yet!</td>

</tr>

";

}

//else, show them!

else

{

//loop through them

while($row = mysql_fetch_array($comments))

{

echo "

<tr>

<td colspan='2'>Comment by: <b>$row[submitter]</b></td>

</tr>

<tr>

<td colspan='2' style='border: 1px solid black; padding: 2px;' vAlign='top'>$row[text]</td>

</tr>

";

}

}

//show the form to enter comments

echo "

<tr>

<td colspan='2'><hr /></td>

</tr>

<form action='$self' method='post'>

<tr>

<td>Name:</td>

<td><input type='text' name='name' maxlength='25'></td>

</tr>

<tr>

<td>Comment:</td>

<td><textarea name='message' cols='40' rows='10'></textarea></td>

</tr>

<tr>

<td colspan='2'><center><input type='submit' name='add_comment' value='Add Comment'></center></td>

</tr>

</form>

";

//-----------------------------

//if the comment submit form

//HAS been submitted, enter info

//to the database.

//-----------------------------

if(isset($_POST['add_comment']))

{

//strip all HTML tags

//and get rid of any quotes to prevent

//SQL injection

$message = mysql_real_escape_string(strip_tags($_POST['message']));

$name = mysql_real_escape_string(strip_tags($_POST['name']));

$time = time();

//use an array to store all error messages

$error_msg = array();

if(empty($message))

{

$error_msg[] = "Please enter a message!<br />";

}

if(empty($name))

{

$error_msg[] = "Please enter a name!<br />";

}

//print the errors

if(count($error_msg)>0)

{

echo "<strong>ERROR:</strong><br>n";

foreach($error_msg as $err)

echo "$err";

}

//else, everything is ok, enter it in the DB

else

{

$query = mysql_query("INSERT INTO tutorials_comments VALUES (NULL,'$id','$name', '$message', '$time')") or die(mysql_error());

}

}

echo "</table>";

}

}


//------------------------------------------

//default case, this is shown default

//in this instance, we are going to make the default case show

//all the categories that you can view tutorials on

//------------------------------------------

?>[/code]

You can see my pages code.Because of the swich when in the adress bar is something like tuts.php?page=4
it just shopws me the tuts.php default
I want to create something like
tuts.php?viewcategory=2?page=5
and to show the right page in the right category,not the index.

I came up with that code:
[code]<?php
include 'connect.php';
include 'header.php';

switch($_GET['action'])

{
//get the id, put it into a variable, cast to an INT
default:
//(for security purposes)
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

// Define the number of results per page
$max_results = 3;

// Figure out the limit for the query based
// on the current page number.
$from = (($page * $max_results) - $max_results);
$id = (int)$_GET['id'];

$query = mysql_query("SELECT * FROM tutorials WHERE cat_id = '1' AND is_validated = '1' ORDER by id DESC LIMIT $from, $max_results ") or die(mysql_error());

//if no results, show that there are no tutorials

//for that category

if(mysql_num_rows($query) == 0)

{

echo "No tutorials for this category yet!";

}

//else, there is..show em

else

{

echo "<center><h1>Photoshop ефекти</h1></center>";

//loop through the tutorials

//show all tutorials

echo "<table border='0' cellpadding='0' cellspacing='0' width='500' align='center'>";

while($row = mysql_fetch_array($query))

{

echo "

<tr>

<td>Заглавие:</td>

<td><b><a href='$self?action=viewtutorial&id=$row[id]'>$row[title]</a></b></td>

</tr>

<tr>

<td>Дата:</td>

<td><b>$row[date_submitted]</b></td>

</tr>

<tr>

<td>Видяно:</td>

<td>$row[views] път(и)</td>

</tr>
<tr>

<td>Гласували:</td>

<td><b>$row[numvotes]</b></td>

</tr>
<tr>

<td>Рейтинг:</td>

<td><font color='red'>$row[avgvotes]</font></td>

</tr>
<tr>

<td>Трудност:</td>

<td><b>$row[difficult]/10</b></td>

</tr>


<tr>

<td>Описание:</td>

<td>$row[short_description]</td>

</tr>

<tr>

<td>Добавил:</td>

<td><a href='http://84.252.50.57/com/index.php?showuser=$row[poster_id]' target='_blank'>$row[submitter]</a></td>

</tr>


<tr>

<td colspan='2' style='border-bottom:2px dotted #000;'><br></td>

</tr>

";

}

echo "</table><br>";

}
// Figure out the total number of results in DB:
$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tutorials"),0);

// Figure out the total number of pages. Always round up using ceil()
$total_pages = ceil($total_results / $max_results);
// Build Page Number Hyperlinks
echo "<center>Изберете страница:<br />";

// Build Previous Link
if($page > 1){
    $prev = ($page - 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\"><<Предишна</a> ";
}

for($i = 1; $i <= $total_pages; $i++){
    if(($page) == $i){
        echo "$i ";
        } else {
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
    }
}

// Build Next Link
if($page < $total_pages){
    $next = ($page + 1);
    echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Следваща>></a>";
}
echo "</center>";

break;
//------------------------------------------

//this case gets the given [ID]

//action=viewtutorial&id=[ID]

//and gets that tutorial ID from the database

//and displays it!

//------------------------------------------


//if there is an ID given..
case "viewtutorial":
if($_GET['id'])

{

//set $id to the URL id, cast to an INT

//for security purposes

$id = (int)$_GET['id'];

//query the database

$query = mysql_query("SELECT * FROM tutorials WHERE id = '$id' LIMIT 1") or die (mysql_error());

//if no rows returned...

if(mysql_num_rows($query) == 0)

{

echo "That ID is not in the database!";

}

//else, show it!

else

{

//update the views for this tutorial!

$update_views = mysql_query("UPDATE tutorials SET views = views + 1 WHERE id = '$id'") or die(mysql_error());

//loop through the database

while($row = mysql_fetch_array($query))

{
echo "

<table border='0' cellpadding='0' cellspacing='0' width='100%' style='text-align:center;'>

<tr>

<td><h2>$row[title]</h2></td>
</tr><tr><td>Този урок е добавен от <a href='http://84.252.50.57/com/index.php?showuser=$row[poster_id]'><b>$row[submitter]</b></a>  с оставен  <a href='mailto:$row[email]'>email</a> за връзка.</td>
</tr>
</table>
<table border='0' cellpadding='0' cellspacing='0' width='100%'>
<tr>

<td colspan='2' style='border-top: 1px dotted black;border-bottom: 1px dotted black;padding-left:3px'><br>$row[text]</td>

</tr>

<tr>

";

//----------------------------

//this part of the code

//checks to see if the submitter

//wants an email left for support

//----------------------------

if($row['show_email'] == 1)

{

echo "

<td colspan='2' style='padding-left:3px'>
Гласувайте:
<form action='ratearticle.php' method='post'>
<input type='hidden' name='id' value='$id '>
<Select name='rankselect'>
       <option>1</option>
         <option>2</option>
         <option>3</option>
        <option>4</option>
        <option>5</option>
         </select><br>
        <input type='submit' name='submit' value='Гласувай!'></form><br><br>
</td>

";

}

echo "

</tr>

<tr>

<td><hr /></td>

</tr>

";

}

//--------------------------------

//this is where we loop through the

//comments table to show all the

//comments for this tutorial

//--------------------------------

$comments = mysql_query("SELECT * FROM tutorials_comments WHERE tut_id = '$id' ORDER BY id DESC") or die (mysql_error());

//if there are no comments..

if(mysql_num_rows($comments) == 0)

{

echo "

<tr>

<td colspan='2' style='padding-left:3px'>Все още няма коментари по този урок!</td>

</tr>

";

}

//else, show them!

else

{

//loop through them

while($row = mysql_fetch_array($comments))

{

echo "

<tr>

<td colspan='2'>Коментар от: <a href='http://84.252.50.57/com/index.php?showuser=$row[poster_id]'><b>$row[submitter]</b></a></td>

</tr>

<tr>

<td colspan='2' style='border-top: 1px dotted black; padding: 2px;border-bottom: 1px dotted black' vAlign='top'>$row[text]</td>

</tr>

";

}

}

//show the form to enter comments
require_once "ipbsdk_class.inc.php";
$SDK =& new IPBSDK();
if ($SDK->is_loggedin()) {
$memberinfo = $SDK->get_advinfo();
echo "

<tr>

<td colspan='2'><hr /></td>

</tr>

<form action='$self' method='post'>

<tr>

<td><input type='hidden' name='posterid' maxlength='25' value=".$memberinfo['id']."></td>
<td><input type='hidden' name='name' maxlength='25' value=".$memberinfo['name']."></td>

</tr>

<tr >

<td >Коментар:</td>

<td><textarea name='message' cols='40' rows='10'></textarea></td>

</tr>

<tr>

<td colspan='2'><center><input type='submit' name='add_comment' value='Добави'></center></td>

</tr>

</form>

";

//-----------------------------

//if the comment submit form

//HAS been submitted, enter info

//to the database.

//-----------------------------

if(isset($_POST['add_comment']))

{

//strip all HTML tags

//and get rid of any quotes to prevent

//SQL injection
$posterid = mysql_real_escape_string(strip_tags($_POST['posterid']));
$message = mysql_real_escape_string(strip_tags($_POST['message']));

$name = mysql_real_escape_string(strip_tags($_POST['name']));

$time = time();

//use an array to store all error messages

$error_msg = array();

if(empty($message))

{

$error_msg[] = "Please enter a message!<br />";

}

if(empty($name))

{

$error_msg[] = "Please enter a name!<br />";

}

//print the errors

if(count($error_msg)>0)

{

echo "<strong>ERROR:</strong><br>n";

foreach($error_msg as $err)

echo "$err";

}

//else, everything is ok, enter it in the DB

else

{

$query = mysql_query("INSERT INTO tutorials_comments VALUES (NULL,'$id','$name', '$message', '$time', '$posterid')") or die(mysql_error());

}

}
}

else {
      // Code for guests goes here
      echo '<font color=red><center>Само регистрираните потребители могат да добавят коментари.</center></font>';
}
echo "</table>";

}

}
break;
}


//------------------------------------------

//default case, this is shown default

//in this instance, we are going to make the default case show

//all the categories that you can view tutorials on

//------------------------------------------
include 'footer.php';
?>[/code]
But as you see there isn't any category listing,so i have to create about 20 files manually and if i want to change them later... OMG.Can you help me with the first code.How it can be done.I need it very much guys.If you help me ... it will be priceless :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.