1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Copying MySQL databases into CSV Format

Discussion in 'MySQL' started by Weirfire, May 16, 2005.

  1. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #21
    Nope it doesn't work :( hehe

    Worth a try. I've written a script to exchange commas for the HTML character code;

    
    <?php
    
    function RemoveComma($code){
    
    $Length = strlen($code);
    
    print "<tr><td><textarea rows='5' cols='70'>";
    for($i=0; $i<$Length; $i++){
    
    
    
    if($code{$i}==","){
    $code{$i}="&";$i++;$code{$i}="#";$i++;$code{$i}="4";$i++;$code{$i}="4";
    }
    
    if($code{$i}=="\n"){
    $code{$i}=="";}
    
    print $code{$i};
    
    }
    
    print "</textarea></td></tr>";
    Return $code;
    
    }
    
    ?>
    
    Code (markup):
    I was also trying to exchange the newline for a space but neither solution works properly. The exchange function just writes on top of the current description as I would have to move everything along first.
     
    Weirfire, May 17, 2005 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #22
    
    <?php
    
    function RemoveComma($code){
    
    $Length = strlen($code);
    
    print "<tr><td><textarea rows='5' cols='70'>";
    for($i=0; $i<$Length; $i++){
    
    
    
    if($code{$i}==","){
    $Carry = $Length - $i;
    
    $codeA = substr($code, 0, $i);
    $codeB = "&#44;";
    $codeC = substr($code, $i+1, $Carry);
    $i=$i+5;
    $code = $codeA.$codeB.$codeC;
    }
    
    if($code{$i}=="\n"){
    $code{$i}=="";}
    
    print $code{$i};
    
    }
    
    print "</textarea></td></tr>";
    Return $code;
    
    }
    
    ?>
    Code (markup):
    Managed to fix the comma problem. Just the newline problem then we're all set :)

    Any ideas how to remove new lines?
     
    Weirfire, May 17, 2005 IP
  3. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #23
    Wouldn't it be easier to do this?


    function RemoveComma($code){
    
         str_replace(",", "&#44", $code);
         str_replace("\n", "", $code);
    
    
    }
    PHP:
     
    palespyder, May 17, 2005 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #24
    Do I need to say $code = str_replace(",", "&#44", $code); ?

    Reason I'm asking is that I tried it the way you said and nothing has happened to the code.
     
    Weirfire, May 17, 2005 IP
  5. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #25
    Yeah sorry about that, do that and echo $code back out and see if it worked.
     
    palespyder, May 17, 2005 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #26
    Ok I did need to assign the str_replace to $code but I still can't get the newline replaced. I've tried replacing \n and & #12;
    I still want to keep the newline when the item is displayed on eBay but the csv doesn't like newlines.
     
    Weirfire, May 17, 2005 IP
  7. mopacfan

    mopacfan Peon

    Messages:
    3,273
    Likes Received:
    164
    Best Answers:
    0
    Trophy Points:
    0
    #27
    Not knowing anything about php, wouldn't it just be easier to write a sql query for what you want?

    Select field1, field4, field10, field11, field17 From SomeTable Where ...

    And then export the results to a csv file?
     
    mopacfan, May 17, 2005 IP
  8. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #28
    WeirFire, can you post what the output of $code is, I only need one line or so.
     
    palespyder, May 17, 2005 IP
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #29
    Thanks for your input mopacfan but what I want to be able to do is select the items which I want to list on eBay by checkboxes on my content management system and then just use the csv which I've written to, to import into the Turbo Lister. It's too much hassle to login to cpanel every time, write a query then export the query table as a csv.

    I'm nearly finished doing it this way actually and only need the newline replaced. :)
     
    Weirfire, May 17, 2005 IP
  10. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #30
    Here's a sample code before any replacements :)

    
    <table width='100%' cellpadding='5' cellspacing='0' border='0' style='background: #6784FF'><tr><td valign ='center' align='center' background='http://www.thepresentshop.co.uk/images/bgpres.jpg' width='250' height='350'><img src='http://www.thepresentshop.co.uk/images/Big/27000.jpg'></td><td valign='top'><table width='100%'><tr><td style='background: #216226; color: white' align='center'><span style='font-size: 150%'>Charles Rennie Macintosh Necklace - P270</span><br><span style='font-size:130%'>Ortak</span></td></tr><tr><td style='font-size: 120%'>Born in Glasgow, Charles Rennie Mackintosh trained as an architect and studied at evening classes in the Glasgow School of Art. At the turn of the century he became one of the forerunners of the distinctive 'Glasgow' style of Art Nouveau. For twenty years he worked as an architect/designer in Glasgow, where all of his best-known work was created and much of it still remains, yet he left the city under-appreciated and died in London in relative obscurity.
    18" Chain
    
    Dimensions: 9x24 mm
    </td></tr><tr><td><hr></td></tr><tr><td style='font-size: 70%'></td></tr><tr><td><hr></td></tr><tr><td>All of <a href='http://www.thepresentshop.co.uk' style='color: white'>The Present Shops</a> items are brand new, sealed in their original containers.</td></tr><tr><td><hr></td></tr><tr><td><table style='background: #E0E0E0' border='1'><tr><td style='font-size: 120%' colspan='4'><h2 align='center'>Postage Costs</h2></td><td rowspan='4' align='center' width='250'><strong>All products over £5.00 will be sent recorded first class. Our aim is to dispatch winning bidders items within 2 days of purchase.</strong></td></tr><tr><th>Within the UK</th><th>Within Europe</th><th>Intl Zone 1</th><th>Intl Zone 2</tr><tr><td align='center'>1.49 GBP</td><td align='center'>2.79 GBP</td><td align='center'>2.99 GBP</td><td align='center'>3.49 GBP</td></tr><tr><td colspan='4'><p align='center'>If you live outside the UK then you may wish to use the <a href='http://www.xe.com/ucc/'>Currency converter</a> to find out how much the postage will cost.</p></td></tr></table></td></tr><tr><td style='color: yellow'><br><strong>Please visit <a href='http://www.thepresentshop.co.uk/' style='color:white'>The Present Shop's</a> website to buy many more items like this 1. Registration on our site will entitle you to a 5% discount on all products.</strong><br> </td></tr><tr><td><table><tr><td><h2>Happy Bidding</h2></td><td>     <img src='http://www.thepresentshop.co.uk/images/Smilie.jpg'></td></tr></table></td></tr></table></td></tr></table></td></tr></table>
    
    Code (markup):
     
    Weirfire, May 17, 2005 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #31
    I have to go catch a plane now but I'll be back on later tonight. :)
     
    Weirfire, May 17, 2005 IP
  12. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #32
    I am going to give you one more thing to try here, try the following:

    $code = preg_replace("/(\r\n|\n|\r)/", "", $code);
    PHP:

    Let me know if this fixes your newline problem.
     
    palespyder, May 17, 2005 IP
  13. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #33
    And if you wanted to replace the newline with a space (I think I read that above- but I was just scanning) do this.
    $code = preg_replace("/(\r\n|\n|\r)/", " ", $code);
    $code = preg_replace("/(\r\n|\n|\r)/", "<BR />", $code); 
    PHP:
     
    exam, May 17, 2005 IP
    palespyder likes this.
  14. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #34
    Great work guys. Thanks for all your help. Just little bits and bobs to tidy up now but those little commands have done the trick :)
     
    Weirfire, May 18, 2005 IP
  15. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #35
    Glad we could help ;)
     
    palespyder, May 18, 2005 IP
  16. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #36
    :) Another little problem....

    I'm using fopen($filename, 'w')) to clear the file before I begin writing to it but it seems to stop writing actual new lines to the file now. All it does is makes those little square things &#12; that represent new lines.

    I'll have a play around with it and see if I can sort it.
     
    Weirfire, May 18, 2005 IP
  17. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #37
    Disregard what I wrote before, can you post the code that writes to the file for you?
     
    palespyder, May 18, 2005 IP
  18. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #38
    
    if (is_writable($filename)) {
    
       // In our example we're opening $filename in append mode.
       // The file pointer is at the bottom of the file hence 
       // that's where $somecontent will go when we fwrite() it.
       if (!$handle = fopen($filename, 'w')) {
             echo "Cannot open file ($filename)";
             exit;
       }
    
       // Write $somecontent to our opened file.
       if (fwrite($handle, $Preamble) === FALSE) {
           echo "Cannot write to file ($filename)";
           exit;
       }
       
       fclose($handle);
    
    } else {
       echo "The file $filename is not writable";
    }
    
    Code (markup):
    That's the first bit that writes to the file. The second part uses "a" instead of "w"
     
    Weirfire, May 18, 2005 IP
  19. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #39
    I think I had to write to the file \n\r instead of just \n. It seems to be doing something now!
     
    Weirfire, May 18, 2005 IP
  20. palespyder

    palespyder Psycho Ninja

    Messages:
    1,254
    Likes Received:
    98
    Best Answers:
    0
    Trophy Points:
    168
    #40
    Sweet, If you have anymore issues let me know ;)
     
    palespyder, May 18, 2005 IP