SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard
    Join Date
    Jul 2006
    Location
    New Zealand
    Posts
    1,300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php file text reciever problem

    Hi all,

    I have a Question i have some data on seperate lines but the data has stuff like this

    1-Citizens-sss-

    1 being the rank id and citizens being rank name as for sss being description what i want to do is how can i input those split those up so when i do a sql query it will b this

    insert into ranks values ('1','citizen','sss')";

    how would i be able to do that dynamically as i have 25 of those so what kind of snippet would i need to do to take a text file and go throw and split up the rank id,rank names and rankdescription into certain variables?

    any help would be gud this is what i have atm not sure how i can get it to do the way i want to do it

    PHP Code:
    include("dbconnect.php");
        
    $rQuery="SELECT * FROM ranks";
        
    $rs=mysqli_query($con,$rQuery);
        if(!
    $rs)
        {
            echo 
    "Error:".mysqli_error($con);
        }
        else
        {
            
    $count=$rs->num_rows;
            if(
    $count>0)
            {                                        
                while(
    $data=$rs->fetch_assoc())
                {
                    
    //
                        
    $rid=$data['rid'];
                        
    $rname=$data['rankname'];
                        
    $rcon=$data['rankcontent'];
                        
    $rreq=$data['recruitreq'];
                        
    $m="be";
                        
    $order="".$rid."-".$rname."-".$rcon."-".$rreq."\n\r";
                        
    $fp=fopen("ranks.txt",'a');
                        if(
    $fp)
                        {
                            
    //echo $order;
                            //echo "<br/>";
                            
    fwrite($fp,$order);
                            
    fclose($fp);
                            
    $pa explode(" "$rid);
                            
    $pb explode(" "$rname);
                            
    $pc explode(" "$rcon);
                            
    $pd explode(" "$rreq);
                            echo 
    "".$pa[0]."".$pb[0]."".$pc[0]."".$pd[0]."<br/>"// piece1
                        
    }
                    
    //        
                
    }
            }
        } 

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You already have the solution right in your code: use explode()

    $parts = explode("-", $string);
    $rank_id = $parts[0];
    $rank_name = $parts[1];
    $description = $parts[2];

  3. #3
    SitePoint Wizard gRoberts's Avatar
    Join Date
    Oct 2004
    Location
    Birtley, UK
    Posts
    2,439
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what about using fgetcsv and using - as the delimeter?

    http://php.net/manual/en/function.fgetcsv.php



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •