הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   עזרה ב- page?. (https://hosts.co.il/forums/showthread.php?t=69064)

Jonathan Zeierman 02-11-08 10:31

עזרה ב- page?.
 
שלום לכולם,
אני מעוניין לעשות דפים שנבנים ככה:
PHP קוד:

?page=Contact 

נאמר לי שמצריך switch get.
אבל מאיפה להתחיל איזה בסיס?
ראו דוגמה:
http://lev-telmond.co.il/?page=Contact
יוני.

omercnet 02-11-08 10:34

יש משתנה שמחזיק את כל הערכים שמתקבלים בURL, שנקרא GET, תעשה
PHP קוד:

var_dump($_GET); 

כדי לראות איך הוא בנוי ואיך לשחק איתו.

שים לב שאסור לך להעביר ערכים ממנו ישר לתוך הקוד, אלא לעשות ווידוא קלט, כי לא תמיד אתה תקבל מה שאתה מצפה לקבל.
זה יכול לגרום לתקיעה פשוטה של האתר, ואפילו לפריצה לכל המערכת שלך - שים לב!

vadimg88 02-11-08 10:52

דוגמא לשימוש:

PHP קוד:


<?php


$page 
trim(stripslashes($_GET['page']));
// $page = intval($page); // if the $page can be only a number uncomment this line


// the array of choices that can be used
$choices = array(

'contact' => 'contact',
'page' => 'page',
'index' => 'index',

);

$loaded = isset($choices[$page]) ? $choices[$page] : 'index'// check that the $page is something that we can use

/// $loaded now holds the desired page do stuff below like:

# Redirect to a page
header('Location: index.php?act='.$loaded);

# Load a file
require_once('path/to/files/'.$loaded);


// etc...



?>


Jonathan Zeierman 02-11-08 12:01

אוקיי הקוד עובד,
אבל הוא מפנה רק ל- Index.
אני לא מבין בזה כל כך איך אני עושה שנגיד ואלחץ על כפתור והוא יזהה איזה כפתור זה ז"א contact וכו'.

vadimg88 02-11-08 12:05

טעות קטנה בקוד זה אמור לעבוד:

PHP קוד:

<?php


$page 
trim(stripslashes($_GET['page']));
// $page = intval($page); // if the $page can be only a number uncomment this line


// the array of choices that can be used
# What to do?
$choice = array(
"contact"        => array( "contact"             'contact'       , array() ),
"page"         => array( "page"             'page'       , array() ),
"index"         => array( "index"             'index'       , array() ),

);


$page = isset($page) ? $page 'index';

if( 
is_array($page) )
{
    
$page 'idx';
}

# Check to make sure the array key exits..
if ( ! isset($choicestrtolower($page) ][0]) )
{
    
$page 'idx';
}

# Redirect to a page
header('Location: index.php?page='.$loaded);

# Load a file
# REQUIRE AND RUN
require( ROOT_PATH $choicestrtolower($page) ][0].".php" ); // load the file xx.php
$runme = new $choicestrtolower($page) ][1]; // call the class xx
$runme->auto_run(); // run it


// etc...



?>

שים לב ששמתי את האלמנט הראשון במערך. זאת אומרת שאם תיגש לקישור index.php?page=contact זה יפנה לקישור index.php?page=contact
במידה ויש לטעון קובץ זה יטען קובץ contact.php

Jonathan Zeierman 02-11-08 12:10

ציטוט:

נכתב במקור על ידי vadimg88 (פרסם 678855)
טעות קטנה בקוד זה אמור לעבוד:

PHP קוד:

<?php


$page 
trim(stripslashes($_GET['page']));
// $page = intval($page); // if the $page can be only a number uncomment this line


// the array of choices that can be used
# What to do?
$choice = array(
"contact"        => array( "contact"             'contact'       , array() ),
"page"         => array( "page"             'page'       , array() ),
"index"         => array( "index"             'index'       , array() ),

);


$page = isset($page) ? $page 'index';

if( 
is_array($page) )
{
    
$page 'idx';
}

# Check to make sure the array key exits..
if ( ! isset($choicestrtolower($page) ][0]) )
{
    
$page 'idx';
}

# Redirect to a page
header('Location: index.php?page='.$loaded);

# Load a file
# REQUIRE AND RUN
require( ROOT_PATH $choicestrtolower($page) ][0].".php" ); // load the file xx.php
$runme = new $choicestrtolower($page) ][1]; // call the class xx
$runme->auto_run(); // run it


// etc...



?>

שים לב ששמתי את האלמנט הראשון במערך. זאת אומרת שאם תיגש לקישור index.php?page=contact זה יפנה לקישור index.php?page=contact
במידה ויש לטעון קובץ זה יטען קובץ contact.php

לא עובד,
http://www.zeierman.co.il/yoni/get.php
והנה הדף contact
http://www.zeierman.co.il/yoni/contact.php

Ron | CSite.co.il 02-11-08 12:24

ואדים הקוד שלך ממש לא חכם,
בכל פעם שהוא ירצה עמוד חדש הוא יצטרך להכנס לדף ולהוסיף טקסט למערך , זה דיי טפשי.
הנה קובץ ה page שלי:

PHP קוד:

//Is this the main page?
if (isset($_GET['page']) && file_exists("sources/src_{$_GET['page']}.php")) 
{    
    
//If exists Class
    
if(file_exists("includes/{$_GET['page']}.class.php"))
        include_once(
"includes/{$_GET['page']}.class.php");
    
//Include sources
    
include_once("sources/src_{$_GET['page']}.php");
    
$template "tpl/master.tpl";
}
else {
    
//If exists Class
    
if(file_exists("includes/index.class.php"))
        include_once(
"includes/index.class.php");
    
//    
    
include_once("sources/src_index.php");
    
$template "tpl/master.tpl";
}
// 

בשביל להוסיף דף אתה פשוט פותח את הדף בתיקייה sources,
בנוסף אם תרצה קובץ מחלקה אתה פותח אותו בתקייה includes.
כמובן שהדף למעלה מאונקלד בתוך קובץ index שעושה פעולות נוספות שיש בהם צורך.

*היתרון הוא שאין צורך להיכנס לקובץ ה page במידה ואתה רוצה להוסיף דף /למחוק.

Jonathan Zeierman 02-11-08 12:35

לא נראה לי הבנתם אותי...
נגיד אני עושה 4 כפתורים:
1.כפתור1.
2.כפתור2.
3.כפתור3.
4.כפתור4.
נגיד ואני לוחץ על כפתור1 אז אני הולך לדף:
index.php?=index
נגיד ואני לוחץ על כפתור2 אז אני הולך לדף:
index.php?=test
וכך הלאה...

Shon12 02-11-08 13:56

ציטוט:

נכתב במקור על ידי Ventrilo ישראל (פרסם 678862)
לא נראה לי הבנתם אותי...
נגיד אני עושה 4 כפתורים:
1.כפתור1.
2.כפתור2.
3.כפתור3.
4.כפתור4.
נגיד ואני לוחץ על כפתור1 אז אני הולך לדף:
index.php?=index
נגיד ואני לוחץ על כפתור2 אז אני הולך לדף:
index.php?=test
וכך הלאה...

אחלה... אז קח את הרעיון של - Ron-Pro, ואיתו אתה עושה קישורים, למשל:
HTML קוד:

<a href="http://site.com/index.php?act=test">test page</a>
<a href="http://site.com/index.php?act=index">test page</a>

וכו'... גם את זה אפשר לעשות אוטמטי. לקרוא נתונים מתייקיה מסיומת -> להכניס למערך -> לולאה -> להדפיס קישורים.

Ron | CSite.co.il 02-11-08 14:19

ציטוט:

נכתב במקור על ידי Ventrilo ישראל (פרסם 678862)
לא נראה לי הבנתם אותי...
נגיד אני עושה 4 כפתורים:
1.כפתור1.
2.כפתור2.
3.כפתור3.
4.כפתור4.
נגיד ואני לוחץ על כפתור1 אז אני הולך לדף:
index.php?=index
נגיד ואני לוחץ על כפתור2 אז אני הולך לדף:
index.php?=test
וכך הלאה...

הרעיון השלי עונה בדיוק על הדרישות שלך,
אבל לשדעתי בכלל לא ניסית להבין את הקוד.
תסתכל מה הקוד עושה,תנסה ואני מבטיח לך שתצליח!


כל הזמנים הם GMT +2. הזמן כעת הוא 19:38.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ