PHP Help [Experts pls]

lkzombie

Well-known member
  • Feb 24, 2013
    2,585
    1,113
    113
    technoext.blogspot.com
    මේ තියෙන්නෙ මම ලියපු class එකක්
    PHP:
    <?php
    include_once get_include_path().'cls/database/cls_dbconnection.php';
    include_once get_include_path().'cls/misc/cls_images.php';
    include_once get_include_path().'cls/misc/cls_session.php';
    include_once get_include_path().'cls/business/cls_users.php';
    
    class _ads
    {
        public $_adid           = "";
        public $_userid         = "";
        public $_subcatid       = "";
        public $_typeid         = "";
        public $_title          = "";
        public $_description    = "";
        public $_price          = "";
        public $_city           = "";
        public $_pictures       = null;
    
        
        public $_message        = "";
        
        
        public function _open()
        {
             try
            {
                $classDB        = new database();
                $dbcon          = $classDB->_getDBconnection();
                $resultset      = null;
                
                if($dbcon)
                {
                    $resultset = $dbcon->prepare("SELECT * FROM tbl_ads WHERE _adid=:_adid AND _status=1;");
                    $resultset->execute(array(':_adid'=>$this->_adid));
                    
                    $rows = $resultset->fetch(PDO::FETCH_ASSOC);
                    
                    $this->_adid           = $rows['_adid'];
                    $this->_userid         = $rows['_userid']; 
                    $this->_subcatid       = $rows['_subcatid'];
                    $this->_typeid         = $rows['_typeid'];
                    $this->_title          = $rows['_title'];
                    $this->_description    = $rows['_description'];
                    $this->_price          = $rows['_price'];
                    $this->_city           = $rows['_city'];
                    
                    $resultset      = null;
                    $resultset = $dbcon->prepare("SELECT * FROM tbl_ads_images WHERE _adid=:_adid;");
                    $resultset->execute(array(':_adid'=>$this->_adid));
                    
                    if(!$resultset->rowCount()==0)
                    {
                        $this->_pictures   = $resultset;
                    }
                    
        
                                    
                    $dbcon = null;
                    $classDB->close_db();
                    return(true);
                }
            }
            catch(exception $e)
            {
                $this->_message="Unexpected error:_open";
                return (false);
            }
        }
        
        public function _save($userinfo="")
        {
            try
            {
                $classDB        = new database();
                $classImage     = new _images();
                $dbcon          = $classDB->_getDBconnection();
                $resultset      = null;
                
                if($dbcon)
                {
                    $dbcon->beginTransaction();
                    
                    //Check user
                    $resultset = $dbcon->prepare("SELECT * FROM tbl_users WHERE _userid=:_userid AND _status=1;");
                    $resultset->execute(array(':_userid'=>$this->_userid));
                        
                    if($resultset->rowCount()!==1)
                    {
                        $this->_message="Invalid user!!"; 
                        $dbcon->rollBack();
                        return (false);
                    }  
                                   
                    //Generate new id
                    $resultset = $dbcon->prepare("SELECT _adid FROM tbl_parameters;");
                    $resultset->execute();
                    
                    $rows = $resultset->fetch(PDO::FETCH_ASSOC);
                    $this->_adid = $rows['_adid']+1;
                    
                    $resultset=null;
                    
                    //Update new id
                    $resultset = $dbcon->prepare("UPDATE tbl_parameters SET _adid=:_adid;");
                    $resultset->execute(array(':_adid'=>$this->_adid));
                    
                    if($resultset->rowCount()!==1)
                    {
                        $this->_message="System error: Parameter cannot be updated."; 
                        $dbcon->rollBack();
                        return (false);
                        exit;
                    }   
                    
                    $resultset=null;
                                  
                    $resultset = $dbcon->prepare("INSERT INTO tbl_ads (_adid, _userid, _subcatid, _typeid, _title, _description, _price, _city, _status) VALUES (?,?,?,?,?,?,?,?,1);");
                    
                    $resultset->bindParam(1, $this->_adid); 
                    $resultset->bindParam(2, $this->_userid);
                    $resultset->bindParam(3, $this->_subcatid); 
                    $resultset->bindParam(4, $this->_typeid); 
                    $resultset->bindParam(5, $this->_title); 
                    $resultset->bindParam(6, $this->_description); 
                    $resultset->bindParam(7, $this->_price); 
                    $resultset->bindParam(8, $this->_city); 
                    
                    $resultset->execute();
                    
                    
                    if($resultset->rowCount()==1)
                    {
                        $resultset          = null;
                        $uploadingpath      = get_include_path().'images';
                        $allowedFileTypes   = array("image/jpg","image/jpeg","image/bmp","image/gif","image/png");
                        
                        for($i=0; $i<count($this->_pictures['name']); $i++) 
                        {
                            echo($i);
                            
                            $tmpFilePath = $this->_pictures['tmp_name'][$i];
                            if($tmpFilePath != "")
                            {
                                $fileType       = $this->_pictures['type'][$i];
                                $ext            = end((explode(".", $this->_pictures['name'][$i])));
                                $filename       = $_SESSION['_user']['id'].'-'.time().'-'.$i.'.'.$ext;
                                
                            
                                $shortname = $this->_pictures['name'][$i];
                                $filePath = "$uploadingpath/uploaded/$filename";
                                
                                 
                                if(!in_array($fileType,$allowedFileTypes))
                                {
                                    $this->_message = 'Image type not allowed! Please upload JPG, JPEG, BMP, GIF or PNG images';
                                    $dbcon->rollBack();
                                    return(false);
                                    exit;
                                }
                                
                                if ($this->_pictures['error'][$i] > 0)
                                {
                                    $this->_message = 'Invalid Image! Unreadable or Damaged picture';
                                    $dbcon->rollBack();
                                    return(false);
                                    exit;
                                }
                                
                                
                                if(!move_uploaded_file($tmpFilePath, $filePath)) 
                                {
                                    $this->_message = 'Internal Error! Please contact webmaster';
                                    $dbcon->rollBack();
                                    return(false);
                                }
                                
                                //100px resized images
                                $classImage->_load("$uploadingpath/uploaded/$filename");
                                $classImage->_resizeToWidth(100);
                                $classImage->_save("$uploadingpath/processed/100/$filename");
                                
                                //400px resized images
                                $classImage->_load("$uploadingpath/uploaded/$filename");
                                $classImage->_resizeToWidth(400);
                                $classImage->_save("$uploadingpath/processed/400/$filename");
                                
                                //800px resized images
                                $classImage->_load("$uploadingpath/uploaded/$filename");
                                $classImage->_resizeToWidth(800);
                                $classImage->_save("$uploadingpath/processed/800/$filename");
                                
                                
                                $resultset = $dbcon->prepare("INSERT INTO tbl_ads_images (_adid, _imagepath, _order) VALUES (_adid=:_adid,_imagepath=:_imagepath,_order=:_order);");
                                $resultset->execute(array(
                                                            ':_adid'        => $this->_adid,
                                                            ':_imagepath'   => $filename,
                                                            ':_order'       => $i
                                                          ));
                                
                                
                            }
                        }
                    }
                                    
                    $dbcon->commit();
                    Return (True);
                    exit;
                }
                else
                {
                    $this->_message="No active database connection ";
                    return (false);
                    exit;
                }
            }
            catch(exception $e)
            {
                $this->_message="Unexpected error:_open ".$e;
                //$dbcon->rollBack();
                return (false);
                exit;
            }
            
            
        }
        
        public function _update()
        {
            
        }
        
        public function _delete()
        {
            
        }
        
        
        
        
        
        
    }
    ?>

    මේ ෆෝම් එක
    PHP:
    <form class="form-horizontal" enctype="multipart/form-data" method="post">
                        <fieldset>
                         
                            <div class="form-group">
                                <label class="col-md-3 control-label">Category <sup>*</sup></label>
                                <div class="col-md-8">
                                    <select title="Select Category" name="_subcatid" id="category-group" class="selectpicker" required=""><!--#category-optionlist#--></select>
                                </div>
                            </div>
                             
                            <div class="form-group">
                                <label class="col-md-3 control-label">Add Type</label>
                                <div class="col-md-8">
                                    <label class="radio-inline" for="radios-0"><input name="_typeid" id="radios-0" value="0" checked="checked" type="radio">Private </label>
                                    <label class="radio-inline" for="radios-1"><input name="_typeid" id="radios-1" value="1" type="radio">Business </label>
                                </div>
                            </div>
                             
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="Adtitle">Ad title <sup>*</sup></label>
                                <div class="col-md-8">
                                    <input id="Adtitle" name="_title" placeholder="Ad title" class="form-control input-md" required="" type="text">
                                    <span class="help-block">A great title needs at least 60 characters. </span>
                                </div>
                            </div>
                             
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="textarea">Describe ad <sup>*</sup></label>
                                <div class="col-md-8">
                                    <textarea class="form-control" id="textarea" name="_description" placeholder="Describe what makes your ad unique" required=""></textarea>
                                </div>
                            </div>
                             
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="Price">Price <sup>*</sup></label>
                                <div class="col-md-4">
                                    <div class="input-group"><span class="input-group-addon">Rs.</span>
                                        <input id="Price" name="_price" class="form-control" placeholder="Price" required="" type="text">
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="checkbox">
                                        <label><input type="checkbox">Negotiable </label>
                                    </div>
                                </div>
                            </div>
                            
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="seller-Location">Location <sup>*</sup></label>
                                <div class="col-md-8">
                                    <input id="autocomplete-ajax" name="_city" class="form-control" required="">
                                
                                </div>
                            </div>
                             
                            <div class="form-group">
                                <label class="col-md-3 control-label" for="textarea"> Pictures</label>
                                <div class="col-md-8">
                                    <div class="mb10">
                                        <input id="_pictures" name="_pictures[]" type="file" class="file"  accept="image/*" multiple="">
                                    </div>
                                    <p class="help-block">Add up to 5 photos. Maximum 2Mb</p>
                                </div>
                            </div>
                            <!--#sellerinformations#-->
                            <div class="form-group">
                                <label class="col-md-3 control-label"></label>
                                <div class="col-md-8">
                                    <div class="termbox mb10">
                                        <label class="checkbox-inline" for="checkboxes-1"><input name="checkboxes" id="checkboxes-1" value="1" type="checkbox" required="">I have read and agree to the <a href="terms-conditions.html">Terms & Conditions</a></label>
                                    </div>
                                    <p class="help-block" id="errorcode"><!--#output_message#--></p>
                                    <div style="clear:both"></div>
                                    <input type="submit" class="btn btn-success btn-lg" title="Register" value="Post Ad"/>
                                </div>
                            </div> 
                            
                        </fieldset>
                    </form>

    මෙතනදි තමයි class එක කෝල් වෙන්නෙ

    PHP:
    if(isset($_SESSION['_user']))
            {
                $classads->_adid           = 0;
                $classads->_userid         = $_SESSION['_user']['id'];
                $classads->_subcatid       = $_POST['_subcatid'];
                $classads->_typeid         = $_POST['_typeid'];
                $classads->_title          = $_POST['_title'];
                $classads->_description    = $_POST['_description'];
                $classads->_price          = $_POST['_price'];
                $classads->_city           = $_POST['_city'];
                $classads->_pictures       = &$_FILES['_pictures'];
                
                
                if($classads->_save())
                {
                    //header('Location: /user/manage-ads.html');
                }
                else
                {
                    display_message($classads->_message);
                }
            }


    මට තියෙන ප්‍රශ්නෙ මේ ලූප් එක හරියට දුවනව ඒ වුනාට ඩේටාබේස් එකට එක රෝ එකයි වැටෙන්නෙ

    PHP:
    for($i=0; $i<count($this->_pictures['name']); $i++) 
                        {
                            echo($i);
                            
                            $tmpFilePath = $this->_pictures['tmp_name'][$i];
                            if($tmpFilePath != "")
                            {
                                $fileType       = $this->_pictures['type'][$i];
                                $ext            = end((explode(".", $this->_pictures['name'][$i])));
                                $filename       = $_SESSION['_user']['id'].'-'.time().'-'.$i.'.'.$ext;
                                
                            
                                $shortname = $this->_pictures['name'][$i];
                                $filePath = "$uploadingpath/uploaded/$filename";
                                
                                 
                                if(!in_array($fileType,$allowedFileTypes))
                                {
                                    $this->_message = 'Image type not allowed! Please upload JPG, JPEG, BMP, GIF or PNG images';
                                    $dbcon->rollBack();
                                    return(false);
                                    exit;
                                }
                                
                                if ($this->_pictures['error'][$i] > 0)
                                {
                                    $this->_message = 'Invalid Image! Unreadable or Damaged picture';
                                    $dbcon->rollBack();
                                    return(false);
                                    exit;
                                }
                                
                                
                                if(!move_uploaded_file($tmpFilePath, $filePath)) 
                                {
                                    $this->_message = 'Internal Error! Please contact webmaster';
                                    $dbcon->rollBack();
                                    return(false);
                                }
                                
                                //100px resized images
                                $classImage->_load("$uploadingpath/uploaded/$filename");
                                $classImage->_resizeToWidth(100);
                                $classImage->_save("$uploadingpath/processed/100/$filename");
                                
                                //400px resized images
                                $classImage->_load("$uploadingpath/uploaded/$filename");
                                $classImage->_resizeToWidth(400);
                                $classImage->_save("$uploadingpath/processed/400/$filename");
                                
                                //800px resized images
                                $classImage->_load("$uploadingpath/uploaded/$filename");
                                $classImage->_resizeToWidth(800);
                                $classImage->_save("$uploadingpath/processed/800/$filename");
                                
                                
                                $resultset = $dbcon->prepare("INSERT INTO tbl_ads_images (_adid, _imagepath, _order) VALUES (_adid=:_adid,_imagepath=:_imagepath,_order=:_order);");
                                $resultset->execute(array(
                                                            ':_adid'        => $this->_adid,
                                                            ':_imagepath'   => $filename,
                                                            ':_order'       => $i
                                                          ));
                                
                                
                            }
                        }



    උදවු කරන අයට 5 ගානෙ සලකනු ලැබේ :rolleyes:
     
    Last edited:
    • Like
    Reactions: kolavari

    RCROCKS

    Well-known member
  • Sep 2, 2008
    2,076
    117
    63
    in my mobile
    loop eka ube pictures ganata loop eka iterate wenawada?
    DB ekata save wenne 1st image ekada last image ekada ehema nathnam random da?
     

    thilaapiya

    Well-known member
  • Oct 16, 2015
    1,062
    287
    83
    වදින්නෙ අන්තිම ඩේටා රෝ එකද? මේකේ ට්‍රාන්සෑක්ශන් එක පටන් ගත්තු එක සේව් එකට පස්සෙ කමිට් වෙන්න ඕනි.මම ඒ ලයින් එක දෑක්කේ නෑ
     

    lkzombie

    Well-known member
  • Feb 24, 2013
    2,585
    1,113
    113
    technoext.blogspot.com
    loop eka ube pictures ganata loop eka iterate wenawada?
    DB ekata save wenne 1st image ekada last image ekada ehema nathnam random da?

    1st row eka witharai. mama database eke save karanne image name eka witharai. :nerd:

    වදින්නෙ අන්තිම ඩේටා රෝ එකද? මේකේ ට්‍රාන්සෑක්ශන් එක පටන් ගත්තු එක සේව් එකට පස්සෙ කමිට් වෙන්න ඕනි.මම ඒ ලයින් එක දෑක්කේ නෑ

    class eke anthimata commit karala ban thiyenne:dull:
     

    p.senanayaka

    Junior member
  • May 15, 2014
    51
    5
    8
    මචන් උබ ලූප් කර කරද බන් ඩේටා දාන්න යන්නේ එහෙම නම් ලූප් එක දාපන් මෙතන්දි පලවෙනි අර්‍රය් එකෙ ඩේටාඅ විතරයි වදින්නේ ලූප් කර කර ගහපන්
     

    lkzombie

    Well-known member
  • Feb 24, 2013
    2,585
    1,113
    113
    technoext.blogspot.com
    මචන් උබ ලූප් කර කරද බන් ඩේටා දාන්න යන්නේ එහෙම නම් ලූප් එක දාපන් මෙතන්දි පලවෙනි අර්‍රය් එකෙ ඩේටාඅ විතරයි වදින්නේ ලූප් කර කර ගහපන්

    one to many relation ekak ban thiyene. detail eka loop karanna wenawa. array ekakata dala try ekak deela balanna ona
     

    The_Killer

    Well-known member
  • Jan 20, 2014
    11,051
    2,196
    113
    WA, Straya 🇦🇺🦘
    www.elakiri.com
    Check this out machan. Kalekin PHP alluwe na. So help ekak denna amarui direct.

    http://stackoverflow.com/questions/19106963/php-prepared-statements-and-transactions-in-a-loop


    Honestly mama karanam karanne first image files tika filter karala (correct size, image format etc..) e names tika array ekakata save karala just one shot DB save karana eka. Meken wenne images ganata connection requests hadenawa. godak connection requests hadena eka db optimization walata awul ne. So first verify your images and upload the data array later.
     
    Last edited:
    • Like
    Reactions: lkzombie

    lkzombie

    Well-known member
  • Feb 24, 2013
    2,585
    1,113
    113
    technoext.blogspot.com
    Check this out machan. Kalekin PHP alluwe na. So help ekak denna amarui direct.

    http://stackoverflow.com/questions/19106963/php-prepared-statements-and-transactions-in-a-loop


    Honestly mama karanam karanne first image files tika filter karala (correct size, image format etc..) e names tika array ekakata save karala just one shot DB save karana eka. Meken wenne images ganata connection requests hadenawa. godak connection requests hadena eka db optimization walata awul ne. So first verify your images and upload the data array later.

    array ekakin database ekata danna puluwan e unata image process karanna thawa loop ekak danna wenawa (eg: resize, add watermark etc..)
     

    kolavari

    Well-known member
  • Aug 11, 2012
    33,746
    1
    25,653
    113
    කැළෑ පොජ්ජේ
    loop eka wada mage class eke mama echo ekak dala thiyenne eka hariyata print wenawa.

    echo එක මචන් තියෙන්නේ ලූප් එකට උඩින් නේ...? :confused::confused:

    PHP:
     for($i=0; $i<count($this->_pictures['name']); $i++)  
                        { 
                            echo($i); 
                             
                            $tmpFilePath = $this->_pictures['tmp_name'][$i]; 
                            if($tmpFilePath != "") 
                            { 
                                    //මේක ඇතුලට ලූප් එක එනවද දන්නේ නැහැ නේ? :confused::confused: මෙතනින් echo එකක් දාල බැලුවොත්? 
                               
                                $fileType       = $this->_pictures['type'][$i]; 
                                $ext            = end((explode(".", $this->_pictures['name'][$i]))); 
                                $filename       = $_SESSION['_user']['id'].'-'.time().'-'.$i.'.'.$ext;

    ඔය මම දාල තියෙන තැනින් එච්හෝ එකක් දාල ප්‍රින්ට් වෙනවද බැලුවොත් මොකෝ බන්? සමහරවිට $tmpFilePath එක සෙට් වෙන්නේ නැද්ද දන්නේ නැහැ ? :confused:
     
    Last edited:

    kolavari

    Well-known member
  • Aug 11, 2012
    33,746
    1
    25,653
    113
    කැළෑ පොජ්ජේ
    එහෙම නැත්තම් INSERT එක මචන් එකක් හරියට යනවා කියන්නේ අනිත් ඒවායේ එක්කෝ ලූප් එකට සෙට් වෙන්නේ නැහැ...:confused: නැත්තම් mysql වල error එකක් පනිනවා වෙන්න ඇති...:baffled:

    උඹේ PDO එකේ execute(); එකට යටින් $resultset->rowCount(); එක ප්‍රින්ට් කරලා බැලුවොත් මොකෝ? එතකොට උබට බලන්න පුළුවන් එකින් එකට ලූප් එකේ හරියට ඉන්සර්ට් වුනාද කියල...?

    $resultset->execute(array(
    ':_adid' => $this->_adid,
    ':_imagepath' => $filename,
    ':_order' => $i
    ));

    echo $resultset->rowCount();
     

    hussainanver52

    Active member
  • Aug 18, 2012
    292
    111
    43
    log ekak dapan save function eka gava log value ekata adid eka hari image name eka hari dapan
    check karapan log count eka
     

    lkzombie

    Well-known member
  • Feb 24, 2013
    2,585
    1,113
    113
    technoext.blogspot.com
    එහෙම නැත්තම් INSERT එක මචන් එකක් හරියට යනවා කියන්නේ අනිත් ඒවායේ එක්කෝ ලූප් එකට සෙට් වෙන්නේ නැහැ...:confused: නැත්තම් mysql වල error එකක් පනිනවා වෙන්න ඇති...:baffled:

    උඹේ PDO එකේ execute(); එකට යටින් $resultset->rowCount(); එක ප්‍රින්ට් කරලා බැලුවොත් මොකෝ? එතකොට උබට බලන්න පුළුවන් එකින් එකට ලූප් එකේ හරියට ඉන්සර්ට් වුනාද කියල...?

    $resultset->execute(array(
    ':_adid' => $this->_adid,
    ':_imagepath' => $filename,
    ':_order' => $i
    ));

    echo $resultset->rowCount();

    1. for loop eka athule echo eka dala thiyenne. eke resalt eka 1 2 3 4 5 kiyala hariyata enawa

    2. data base ekata 1st row eka add wenawa commit ekath run wenawa

    3. row count eka enne 1i (meka thamai awula.)

    4. Images hariyatama upload wela resize wela watermark add wela enawa
     

    kolavari

    Well-known member
  • Aug 11, 2012
    33,746
    1
    25,653
    113
    කැළෑ පොජ්ජේ
    1. for loop eka athule echo eka dala thiyenne. eke resalt eka 1 2 3 4 5 kiyala hariyata enawa

    2. data base ekata 1st row eka add wenawa commit ekath run wenawa

    3. row count eka enne 1i (meka thamai awula.)

    4. Images hariyatama upload wela resize wela watermark add wela enawa

    machan sorry, typing mistake ekak mama loop kiyala dala thiyenne :D

    loop eka athule thamaa, but mama mean kare machan loop eka kohomath run wenawa ne :rolleyes: ethakota echo eka print wenawa....

    echo eka dammoth moko exectute() eka lagin :D ethakota ethanata enawada kiyala balaganna puluwan :yes:

    nattham ara deweni post eke thiyena eka balanna