הרשם שאלות ותשובות רשימת חברים לוח שנה הודעות מהיום

חזור   הוסטס - פורום אחסון האתרים הגדול בישראל > עיצוב גראפי, תכנות על כל שפותיו וקידום ושיווק אתרים > פורום תיכנות

   
|!|

השב
 
כלים לאשכול תצורת הצגה
ישן 22-01-09, 22:53   # 1
Erez | TrustMedia.co.il
עסק רשום [?]
 
מיני פרופיל
תאריך הצטרפות: Jul 2008
הודעות: 1,854

Erez | TrustMedia.co.il לא מחובר  

עזרה|האם אני עובד נכון?

לא מזמן למדתי מחלקות והיום התחלתי להשתמש בהם.
בניתי מחלקת טמפלייטים והרשמה ואני רוצה לדעת האם אני עובד נכון או שלא.
הנה הקודים של המערכת:
reg.php:

PHP קוד:
<?php
include 'classes.php';
$tpl=new tpl('html.tpl');
$tpl->timer_start();
$reg=new register();
if(isset(
$_POST['submit'])){
    if(
$reg->check($_POST['username'],$_POST['password'],$_POST['repassword'],$_POST['email'])){
        if(
$reg->insert()){
            
$content .= 'נרשמת בהצלחה לאתר.';
        }
    }
    else{
        
$content .= $reg->get_error();
        
$content .= $reg->form();
    }
}
else{
    
$content .= $reg->form();
}
$tpl->set('title','הרשמה');
$tpl->set('body',$content);
$tpl->timer_done();
echo 
$tpl->end();
?>
classes.php:
PHP קוד:
<?php
class tpl{
    var 
$tpl_file 'html.tpl';
    var 
$tags = array();
    var 
$start'';
    function 
tpl($tp_file=true){
        if(
$tpl_file){$this->tpl_file=$tpl_file;}
    }
    function 
timer_start(){
        
$this->start microtime(true);
     }
    function 
timer_done(){
        
$time_end microtime(true);
        
$time $time_end $this->start;
        
$this->tags['porgress']='זמן טעינת העמוד '.substr($time,0,10);
    }
    function 
set($tag,$content){
        
$this->tags[$tag]=$content;
    }
    function 
end(){
        
$tpl=file_get_contents($this->tpl_file);
        foreach(
$this->tags as $tag => $content){
            
$tpl=str_replace('{'.$tag.'}',$content,$tpl);
        }
        return 
$tpl;
    }
}

class 
register{
    var 
$id='';
    var 
$username='';
    var 
$password='';
    var 
$repassword='';
    var 
$email='';
    function 
form(){
        if(isset(
$_POST['submit'])){
            
$username htmlspecialchars($_POST['username']);
            
$email htmlspecialchars($_POST['email']);
        }
        else{
            
$username '';
            
$email '';
        }
        
$code ='<form method="post">
            <table>
                <tr><td>שם משתמש:</td><td><input type="text" name="username" value="'
.$username.'" /></td></tr>
                <tr><td>סיסמא:</td><td><input type="password" name="password" /></td></tr>
                <tr><td>אימות סיסמא:</td><td><input type="password" name="repassword" /></td></tr>
                <tr><td>אימייל:</td><td><input type="text" name="email" value="'
.$email.'" /></td></tr>
                <tr><td><input type="submit" name="submit" value="הרשם" /></td><td><input type="reset" value="אפס" /></td></tr>
                </table>
            </form>'
;
        return 
$code;
    }
    function 
check($username,$password,$repassword,$email){
        
$this->username=htmlspecialchars($username);
        
$this->password=htmlspecialchars($password);
        
$this->repassword=htmlspecialchars($repassword);
        
$this->email=htmlspecialchars($email);
        if(
$this->username == '')return false;
        elseif(
$this->password == '')return false;
        elseif(
$this->repassword == '')return false;
        elseif(
$this->email == '')return false;
        elseif(
$this->password != $this->repassword)return false;
        elseif(!
preg_match('/[a-zA-Z0-9]+@[a-zA-Z0-9+.]+[a-zA-Z0-9]/',$this->email))return false;
        elseif(
strlen($this->username) < || strlen($this->password) < 3)return false;
        elseif(
strlen($this->username) > 30 || strlen($this->password) > 30)return false;
        else return 
true;
    }
    function 
get_error(){
        if(
$this->username == '')return 'לא מילאת את כל שדות ההרשמה.';
        elseif(
$this->password == '')return 'לא מילאת את כל שדות ההרשמה';
        elseif(
$this->repassword == '')return 'לא מילאת את כל שדות ההרשמה';
        elseif(
$this->email == '')return 'לא מילאת את כל שדות ההרשמה';
        elseif(
$this->password != $this->repassword)return 'הסיסמאות שהזנת אינם תואמות.';
        elseif(!
preg_match('/[a-zA-Z0-9]+@[a-zA-Z0-9+.]+[a-zA-Z0-9]/',$this->email))return 'האימייל שהזנת אינו תקין.';
        elseif(
strlen($this->username) < || strlen($this->password) < 3)return 'התוכן שהכנסת בשדות קצר מדי.';
        elseif(
strlen($this->username) > 30 || strlen($this->password) > 30)return 'התוכן שהכנסת בשדות ארוך מדי.';
    }
    function 
insert(){
        
mysql_query("insert into `members` (`username`,`password`,`email`)
        values ('"
.mysql_real_escape_string($this->username)."','".md5($this->password)."','".mysql_real_escape_string($this->email)."')")
        or die(
'Error');
        return 
true;
    }
}
?>
html.tpl:
PHP קוד:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<
html lang="he" xmlns="http://www.w3.org/1999/xhtml" xml:lang="he">
<
head>
    <
meta http-equiv="Content-Type" content="text/html;charset=windows-1255" />
    <
link rel="stylesheet" href="css.css" type="text/css" media="screen"/>
    <
title>{title}</title>
    <
style>
    
body{direction:rtl;font-family:Arial;font-size:10pt;}
    </
style>
</
head>
<
body>
    {
body}<br />
   {
porgress}
</
body>
</
html
יש לציין שזו הפעם הראשונה שעבדתי עם מחלקות ולכן אני רוצה לשנות דברים לא נכונים כבר מעכשיו לפני שאני יתרגל אליהם.
בנוסף בהרשמה חסרים עוד כמה דברים לדוגמא בדיקה אם המשתמש קיים וכו'... אז נא לא להתייחס

אשמח לעזרה מצדכם
__________________
  Reply With Quote
ישן 22-01-09, 23:44   # 2
DvirCohen
חבר בקהילה
 
מיני פרופיל
תאריך הצטרפות: Dec 2007
הודעות: 151

DvirCohen לא מחובר  

האמת שלא בדקתי כ"כ את הקוד, אבל מבחינת המחלקות אתה לא מייעל אותן נכון.

למשל,
מחלקת register צריכה להכיל רק פונקציות שקשורות להרשמת המשתמש, כגון אימות הפרטים (שעשית בסדר), בדיקה אם שם המשתמש/אימייל תפוס, הכנסת פרטי המשתמש למסד וכו'.
הפונקציה form לא צריכה להיות במחלקה הזאת, היא יותר תתאים למחלקה של עיצוב - tpl.

או למשל הפונקציה timer_start במחלקה tpl, זאת ממש לא פונקציה של מחלקת תבניות עיצוב..

אממ ועוד דבר, אני ממליץ לך ללמוד להשתמש במחלקות בPHP5, כרגע לפי מה שאני רואה אתה משתמש כPHP4.
  Reply With Quote
ישן 23-01-09, 14:12   # 3
Erez | TrustMedia.co.il
עסק רשום [?]
 
מיני פרופיל
תאריך הצטרפות: Jul 2008
הודעות: 1,854

Erez | TrustMedia.co.il לא מחובר  

אוקיי אז להעביר את הform למחלקה של העיצוב,להעביר את הטיימר של הדף למחלקה נפרדת וזהו?
ואפשר מדריך למחלקות בPHP5?
__________________
  Reply With Quote
ישן 23-01-09, 14:31   # 4
intercooler3819
חבר וותיק
 
מיני פרופיל
תאריך הצטרפות: Jul 2008
הודעות: 1,056

intercooler3819 לא מחובר  

בPHP5 יש לך אפשרות לשרשר דברים..
נניח
PHP קוד:
$db->get('filename')->from('tablename')->where('ab''equal''cd')->limit(0,10); 
וכד'

ד'א למה להשתמש בSUBSTR? תפעיל SPRINT F
__________________
  Reply With Quote
ישן 23-01-09, 14:36   # 5
Erez | TrustMedia.co.il
עסק רשום [?]
 
מיני פרופיל
תאריך הצטרפות: Jul 2008
הודעות: 1,854

Erez | TrustMedia.co.il לא מחובר  

ציטוט:
נכתב במקור על ידי nitsanbn צפה בהודעה
בPHP5 יש לך אפשרות לשרשר דברים..
נניח
PHP קוד:
$db->get('filename')->from('tablename')->where('ab''equal''cd')->limit(0,10); 
וכד'

ד'א למה להשתמש בSUBSTR? תפעיל SPRINT F
אוקיי,ויש לך מדריך?
ומה בדיוק sprintf עושה?לא ממש הבנתי מphp.net
__________________
  Reply With Quote
ישן 23-01-09, 16:09   # 6
DvirCohen
חבר בקהילה
 
מיני פרופיל
תאריך הצטרפות: Dec 2007
הודעות: 151

DvirCohen לא מחובר  

מה שהוא עשה נקרא chaining, לא בדיוק לזה התכוונתי שדיברתי שתלמד על מחלקות בPHP 5, אבל עושים את זה ע"י החזרת אובייקט פשוט..
PHP קוד:
class letters
{
  public function 
A()
  {
    print 
"A";
    return 
$this;
  }

  public function 
B()
  {
    print 
"B";
    return 
$this;
  }

  public function 
C() 
  {
    print 
"C";
    return 
$this;
  }

  public function 
numbers()
  {
    return new 
numbers;
  }
}

class 
numbers
{
  public function 
one()
  {
    print 
1;
    return 
$this;
  }

  public function 
two()
  {
    print 
2;
    return 
$this;
  }
}

$letters = new letters;
$letters->A()->B()->C()->numbers()->one()->two(); 
וsprintf זאת פונקציה שנותנת לך לעצב מחרוזות, לדוגמא:
PHP קוד:
print sprintf("I have %d %s and %d %s"5"apples"0"oranges"); // I have 5 apples and 0 oranges 

Last edited by DvirCohen; 23-01-09 at 16:12..
  Reply With Quote
ישן 24-01-09, 19:16   # 7
Erez | TrustMedia.co.il
עסק רשום [?]
 
מיני פרופיל
תאריך הצטרפות: Jul 2008
הודעות: 1,854

Erez | TrustMedia.co.il לא מחובר  

אוקיי תודה
ויש למישהו הערות נופות על הקוד שלי?
__________________
  Reply With Quote
השב

חברים פעילים הצופים באשכול זה: 1 (0 חברים ו- 1 אורחים)
 


חוקי פירסום
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is מופעל
סמיילים הם מופעל
[IMG] קוד מופעל
קוד HTML מכובה

קפיצה לפורום


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

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