Jump to content

BagoZonde

Members
  • Posts

    69
  • Joined

  • Last visited

Posts posted by BagoZonde

  1. Thanks Christian, your point about collate is well taken. I changed some collations and charsets but it still not working properly.

    Here's details about my database after few operations you've suggested:

     

     

    [b]CHARACTER SET:[/b]
    Array
    (
       [0] => Array
           (
               [Variable_name] => character_set_client
               [Value] => utf8
           )
    
       [1] => Array
           (
               [Variable_name] => character_set_connection
               [Value] => utf8
           )
    
       [2] => Array
           (
               [Variable_name] => character_set_database
               [Value] => utf8
           )
    
       [3] => Array
           (
               [Variable_name] => character_set_filesystem
               [Value] => binary
           )
    
       [4] => Array
           (
               [Variable_name] => character_set_results
               [Value] => utf8
           )
    
       [5] => Array
           (
               [Variable_name] => character_set_server
               [Value] => latin2
           )
    
       [6] => Array
           (
               [Variable_name] => character_set_system
               [Value] => utf8
           )
    
       [7] => Array
           (
               [Variable_name] => character_sets_dir
               [Value] => /home/mysql55/share/charsets/
           )
    
    )
    
    [b]COLLATION:[/b]
    Array
    (
       [0] => Array
           (
               [Variable_name] => collation_connection
               [Value] => utf8_general_ci
           )
    
       [1] => Array
           (
               [Variable_name] => collation_database
               [Value] => utf8_general_ci
           )
    
       [2] => Array
           (
               [Variable_name] => collation_server
               [Value] => latin2_general_ci
           )
    
    )
    
    
    [b]TABLE:[/b]
    [table_collation] => utf8_unicode_ci
    

     

    What I'm doing it's just a simple submitter for quick-test purposes only of course:

       if ($_POST['string']){
           //GET & SAVE
           print 'Input string: ' . $_POST['string'] . '<br />';
           $sql='UPDATE sites SET content=? WHERE id=?';
           $params=array($_POST['string'], ;
           $result=Database::query($sql, $params);
       }
    
       //CHECK
       $sql='SELECT * FROM sites WHERE id=?';
       $params=array(;
       $result=Database::query($sql, $params);
       print 'Output string: ' . $result[0]['content'];
    
       //WHAT I WANT
       $string='„Quảng Trị”';
       print <<<FORM
       <form action='/iterator' method="POST">
       <textarea name="string">{$string}</textarea>
       <input type="submit" value="Submit">
       </form>
    FORM;
    

     

    I hope there's something simple I've missing. I'm not good in MySQL at all.

    Thanks!

  2. Hello freaks ;), I have issue with converting special chars. This is example of string I'm working with:

     

    „Quảng Trị”

     

    So here we go with bdquo and rdquo quotes and some other stuff with exotic letters (Vietnamese alphabet letters).

     

    Of course I'm working with UTF-8 character set.

     

    And that's what I need to do with this:

     

    1. I'm using some WYSIWYG script for textarea so that exotic letters should be displayed as Decimal NCRs. I'm saving that field to MySQL database (5.5):

     

    [Engine] => InnoDB

    [Version] => 10

    [table_collation] => latin2_general_ci

     

    2. Then I want to retrieve that string from database and display as it is.

     

    I was trying with htmlentities(), html_entity_decode(), mb_encode_numericentity() but I'm still confused. I can get these exotic chars with ord() (for example first quote stands for char: 226, 128, 158) then replace to Decimal NCR or name with ampersand, but I have no time and strength to build whole table with conversions, besides re-inventing wheel it's nothing I need here ;).

     

    I found great site to convert strings so this converter for Decimal NCRs working like a charm: http://rishida.net/tools/conversion/

     

    I hope my problem is clearly explained and there's some solution for such things.

    Thanks for interesting!

  3. Sorry, that quick answers without test are really bad. Try with this:

     

        foreach($form as $input){
           if ($_POST[$input]!=''){
               $field[$input]=$_POST[$input];
           }else{
               $error[$input]=1;
           }
       }

     

    instead of that:

     

    foreach($form as $input){
       $field[$input]=$_POST[$input];
       if (!$field[$input]){
           $error[$input]=1;
       }
    }

  4. NIH Syndrome, yup, lovely NIH Syndrome ;). I remember that thanks to jQuery I discovered how to kick some ready (and working (!)) solutions into my projects. And my JS scripts finally works on every developed browser, no frustrations anymore :]. Of course sometimes some browser version refused to work with jQuery properly (last time I have some issues with Chrome and Opera), but with some experience, jQuery is a charm.

  5. It's because your script don't save $enemy_hp. PHP is server scripting language that means it's executed on server side so your code is terminated. You must save values to text file or better would be write it to database and then when script is executed, it must read $enemy_hp value. Maybe you would be interested with javascript and jQuery to do such things because variables are still available in memory of your computer.

  6. Sorry jharvie, I didn't tested that code! Please change that line:

     

     	 }else{
    $range=array(

     

    to

     

     }else if (!$error){
    $range=array(
    
    

     

    Sorry for that mistake.

     

    BTW. I've used in_array() as cpd suggested, so I hope he will not be mad at all ;].

  7. So this solves most of my issue. Now the only thing I am trying to figure out is how to make sure that there is an entry in the First Name, Last Name and Course number boxes, and if not, then I have to have it display the error message. I'm sure this is probably quite simple to do, although I cant seem to figure out what needs to be added in. If I enter just the last name, course number and lets say 89 as a grade, it also displays the table of results with an empty first name, but as well as displays the error page below the table, saying Please fill out all fields. However, it appends the incomplete submission to the text file.

     

    Maybe you missed that incorrect fields are styled with red border (you can change it in $defined_error variable) so form is displayed for correction. If you want to add text, just enter under that line or somewhere there:

     

    if ($error || !$_POST || $_POST['clear']){

     

    if ($error){
    print 'Please fill up fields properly';
    }
    

     

    @cpd

    I don't wrote that stuff from scratch, just edit what I had from jharvie, copy&paste and modified some things. About $_POST that's right, it's just an easy way, but as you see for that simple example - we're passing $form values in foreach loop so only few variables can really go through this code.

     

    That's a quick solution which is okay in my opinion, however sorry if you're thinking I'm ignoring your tips. If you want to, just sit and write your piece of code for form submission.

  8. Here you go with whole process_EnterGrades.php script. No script for clearing needed.

     

    <html>
    <head>
    <title> Enter Course Grades </title>
    <link rel='stylesheet' type='text/css' href='background.css' />
    </head>
    <body>
    <center><img src='Logo.jpg' alt='Company Logo' width='450' height=150'></center>
    <br>
    <center>
    
    <?php
    // This defines the name of the file to be saved with the user information and grades
    $Savedfile = "FinalGrades.txt";
    
    
    $field=array();
    
    if ($_POST){
       //if form submitted
       $defined_error=' style="border: 2px solid #aa2200;"';
    
       if ($_POST['clear']){
           //This clears the form fields if it is reset and... that's all!
       }else{
           //Defines the form variables
           $form=array('firstname', 'lastname', 'coursenumber', 'finalgrade');
           $error='';
           foreach($form as $input){
               $field[$input]=$_POST[$input];
               if (!$field[$input]){
                   $error[$input]=1;
               }
           }
           if (!is_numeric($field['finalgrade'])){
               $error['finalgrade']=1;
           }else if ($field['finalgrade']<0 || $field['finalgrade']>100){
               $error['finalgrade']=1;
           }else{
               $range=array(
                   'F'=>array(0, 59),
                   'D'=>array(60, 69),
                   'C'=>array(70, 79),
                   'B'=>array(80, 85),
                   'A-'=>array(86, 90),
                   'A'=>array(91, 95),
                   'A+'=>array(96, 100),
    
               );
               foreach($range as $finalgrade_letter=>$finalgrade_range){
                   //print 'range from ' . $finalgrade_range[0] . ' to ' . $finalgrade_range[1] . '<br />'; //uncomment this line if you're confused
                   $range_array=range($finalgrade_range[0], $finalgrade_range[1]);
                   if (in_array($field['finalgrade'], $range_array)){
                       break; //letter found, so let's get out of here
                   }
               }
               echo "<br>";
               echo "<br>";
               echo "<br>";
               echo "<br>";
    
               //Data for displaying
               $StringData = '<tr><td>First Name: ' . $field['firstname'] . '</td>';
               $StringData.= '<td>Last Name: ' . $field['lastname'] . '</td></tr>';
               $StringData.= '<tr><td>Course Number: ' . $field['coursenumber'] . '</td>';
               $StringData.= '<td>Final Grade: ' . $finalgrade_letter . '</td></tr>';
               print '<center>';
               print '<table border=1 width="50%">';
               print $StringData;
               print '</table>';
               print '</center>';
    
               //Data for document
               //Below will define the format which the fields will be saved to the text document specified at the beginning of this document.
               $StringData = 'First Name: ' . $field['firstname'];
               $StringData.= ' Last Name: ' . $field['lastname'];
               $StringData.= ' Course Number: ' . $field['coursenumber'];
               $StringData.= ' Final Grade: ' . $finalgrade_letter . '\n';
    
               //This opens the text document named FinalGrades.txt and adds the new data. If it cannot open it, it then displays an error message to the user.
               $file = fopen($Savedfile,'a') or die("Cannot open file, check permissions");
               fwrite($file, $StringData);
               fclose($file);
           }
       }
    }
    
    
    if ($error || !$_POST || $_POST['clear']){
    ?>
    
       <h1> Enter Course Grades </h1>
       <h2> Please fill out all fields </h2>
    
    <Form action ='process_EnterGrades.php' method="POST">
    <?php
       $error_class=$error['firstname']?$defined_error:'';
       print 'First Name: <input type="text" name="firstname" size="20" value="' . $field['firstname'] . '"' . $error_class . '>';
    
       $error_class=$error['lastname']?$defined_error:'';
       print 'Last Name: <input type="text" name="lastname" size="20" value="' . $field['lastname']. '"'.$error_class.'>';
    ?>
    <br>
    <br>
    <br>
    <?php
       $error_class=$error['coursenumber']?$defined_error:'';
       print 'Course Number: <input type="text" name="coursenumber" size="20" value="' . $field['coursenumber'] . '"'.$error_class.'>';
    
       $error_class=$error['finalgrade']?$defined_error:'';
       print 'Final grade in percent (%): <input type="text" name="finalgrade" size="20" value="' . $field['finalgrade'] . '"' . $error_class . '>';
    ?>
    </center>
    <br>
    <!-- This creates the submit buttons -->
    <center>
    <table border="3" bordercolor="red">
       <tr>
           <td>
               <input type="submit" value="Submit Your Entries">
           </td>
       </tr>
    </table>
    </form>
    <!-- Below creates a second form that is used to clear the entries -->
    <form name="gradesclear" action="process_EnterGrades.php" method="POST">
    <td>
    <input type="submit" value="Clear Your Entries">
    <input type="hidden" name="clear" value="1">
    </td>
    </tr>
    </FORM>
    
    <?php
    }
    ?>
    
    </center>
    </body>
    </html>
    

  9. I'm not sure what you want to do, but...

     

    Your code will display graduation only if form was accepted (no errors occured). That way you're sure that $finalgrade is numeric coz your code parse all values. If there's something wrong, just set $validate flag to false. Just do it that way:

     

     

    $validate=true;
    //Here your code will pass form entry, if something is wrong just set $validate=false;
    ...
    
    //Here your code will transform numeric grade to letter grade
    if ($validate){ //If no errors after submit
       if ($finalgrade>=0 && $finalgrade<=59){
           $finalgrade='F';
       }else if($finalgrade>=60 && $finalgrade<=69){
           $finalgrade='D';
       }else if(...and so on...){
    
       }else if($finalgrade>=96 && $finalgrade <=100){
           $finalgrade="A+";
       }
    
       //Display grade as letter
       print $finalgrade;
    }else{
      //Display form once again for correction
    }
    
    

  10. You can just use is_numeric() validation, other way is to create white list with select and all available grades as options (however second one needs validation too as somebody could change option values).

    You can also pass only numbers like that:

     

    $grade = preg_replace("/[^0-9]/","", $grade);

     

    One thing: if something is wrong with input data and form is displayed once again for correction, it's cool to import values to inputs and tag input that must be corrected with red border, etc.

  11. EDIT: @BagoZonde, what do you mean there is no $email variable? It is red from the form. And about print_r, I tried and tried before to print it but without success, can you please copy the code I have to insert?

     

    Thank you

     

    Are you sure? Have you checked $email variable before executing mail() function? Just insert

     

    print 'E-MAIL: '.$email;

     

    and see if there's something.

    That same with creating $headers, there's nothing in $email.

     

    Is it your code or you just modified one?

  12. There's too much of code, it's scarry. You should create some array and iterate each input field for generating form and for receive and parse data. Then you would avoid mistakes for sure.

    Why don't you just check what's going on step by step. First try to print_r($_POST) and see what's there, then try to print e-mail message and check variables needed as parameters for your functions. I found one difference with mail function.

     

    There's no $email variable available (BTW no $naslov in mail parameters), you keep it in $_POST['email'] only. So you're trying send mail to anybody.

  13. Thanks for the try BagoZonde, however it doesn't meet requirements as it will only return rows that are not NULL and which the ID matches. FIND_IN_SET is better to use than LIKE for comma separated lists (that's what it's for).

     

    Ooops, sorry, I didn't catch that part with NOT NULL as important one. You're right. FIND_IN_SET return NULL if column is NULL and 0 if not match, so it looks like nice solution. BTW Thanks for advice with FIND_IN_SET, as you see I'm not though in SQL at all.

  14. I'm not sure is the best option but try with this:

     

    $sql='SELECT * FROM table WHERE column LIKE "%' . $user_country_id . '%" ORDER BY some_field';

     

    Of course be cautious about your IDs data. If you're not sure that IDs could be similiar in some way (e.g. when looking for AB and there could be ABC too) you can make double check from records you get by explode values or just use commas even for last element and seek for $user_country_id="AB," instead of $user_country_id="AB".

     

    About optimizing, just kill me, I have no idea what's the best option either. Hope it helps.

  15. Greetings PHP freaks ;]! As this is my first time here, I just want to say hello for all of you! I love PHP so I'm using it everywhere I can (God bless AJAX because I can use PHP there too :D). For now I want to jump on higher level of PHP & MySQL so I'm reading books, especially about optimising and security. I'm working also on some easy framework with some nice features (jQuery included) so it helps me expand my knowledge. And it's true I decided to create it because with Drupal 6/7 my dreams come false - it's not in my style - after one intensive year of work with it I just quit ;]. PHPfreaks seems to be a great opportunity to learn more and help somebody so I'm glad I'm here, I need it :]. I'm also interested with 6502 assembler for Commodore 64 so I'm working on few game projects, it's a cool hobby but I choose PHP as my job path 5 years ago, so PHP is both, job and my hobby :].

     

    Best,

    Bago

     

    P.S. Sorry for my 'Inglish' ;]. I'll try my best :P

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.