Jump to content

hansford

Members
  • Posts

    562
  • Joined

  • Last visited

  • Days Won

    4

Posts posted by hansford

  1.  

     

    ...it has a text field, this field would be automatically completed from what the user typed in at the start. On that URL there is also a button, that is automatically clicked once the text field has been automatically entered.

     

    This can be accomplished, but why would you need to - once the user is out of the picture - using "automatically", then you can just process the form info on one page - what are you really trying to do - hide a URL or something. Tell us the task you really want to accomplish.

  2. You're trying to follow someone else's code which is daunting for even a seasoned developer.

    If you need help finding explanations on PHP functionality - it's everywhere. The best documented programming language in the world. Google any php method, language construct etc.. and you will get a link to the php manual. It will be your best friend.

  3.  

     

    i am trying to learn php, but i find little, if any, explanations in what codes does what function.

     

    WordPress has been pieced together through the years and with all of the plugins - it just makes it that much more difficult to follow the code. You need tools to properly work. You need an IDE with search capabilities. I use Eclipse, as I'm a throwback from Java, but I don't recommend it for PHP. I've read PhpStorm is good, but no first hand knowledge.

  4. Here is an ugly, down and dirty way example. Not downloadable (copy and paste), zero formatting etc..

    <?php
    
    if (isset($_POST['submit']))
    {
        // grab your post values, do DB interactions etc.
        echo 'Form submitted successfuly.';
        exit(); // or redirect to another page
    
    }
    elseif (isset($_POST['xml']))
    {
        $name = $_POST['name'];
        $birthday = $_POST['birthday'];
    
        echo '<pre>';
        echo htmlspecialchars('<?xml version="1.0"?>');
        echo htmlspecialchars('<person>');
        echo htmlspecialchars("<name>$name</name>");
        echo htmlspecialchars("<birthday>$birthday</birthday>");
        echo htmlspecialchars('</person>');
        echo '</pre>';
        exit();
    }
    
    ?>
    <html>
    <head>
    </head>
    <body>
    <form name="form" method="post" action="index.php">
    Name:<input type="text" name="name"><br />
    Birthday:<input type="text" name="birthday"><br />
    <input type="submit" name="submit" value="submit">     
    <input type="submit" name="xml" value="export to xml">
    </form>
    </body>
    </html>
    
    
  5. ginerjm - posted the preferred way to code this - smooth, elegant - with error handling. Just test that rowCount() works on the particular DB you're using. I use rowCount(), but because of the manual "warning", I am in the habit of using fetchColumnt() - however, it is an extra query and slows things down and usually unnecessary.

  6. My apologies, but CroNiX posted the solution for you - here is the code he posted:

    <?php
    include("AddStats_admin_connect.php");
    //connect to database
    doDB();
    //Function to build select options based on passed array
    function buildSelectOptions($options)
    {
      $optionsHTML = '';
      foreach($options as $id => $label)
      {
        $optionsHTML .= "<option value='{$id}'>{$label}</option>\n";
      }
      return $optionsHTML;
    }
    
    //Run query to get the ID and Name from the table
    //Then populate into an array
    $clist_sql = "SELECT CID, Country FROM Countries";
    $clist_res= mysqli_query($mysqli, $clist_sql) or die(mysqli_error($mysqli));
    if (mysqli_num_rows($clist_res) < 1) {
      //this Country not exist
      $display_block = "<p><em>You have selected an invalid Country.<br/>
      Please try again.</em></p>";
    }
    
    $countries = array();
    while($Ctry_info = mysqli_fetch_array($clist_res))
    {
      $countries[$Ctry_info['CID']] = $Ctry_info['Country'];
    }
    $countryOptions = buildSelectOptions($countries);
    ?>
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <title>Stats</title>
    <link rel="stylesheet" href="stylesheets/style.css" />
    <!--[if IE]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    </head>
    <body>
      <form action="CountryOptions.php" method="post">
      <!-- You need to set up the select element to house the options. This will be set in $_POST as $_POST['country'] -->
        <select name="country">
          <?php echo $countryOptions; ?>
        </select>
        <input type="submit" value="Submit Choice">
      </form>
    </body>
    </html>
    
  7. Sorry, I can't test the code for errors and I don't know from your file which line the error occurred.

     

    Looking at it - I see:

    if ($result->fetchcolumn() > 0)
    

    fetchcolumn - should be fetchColumn with a capital 'C'.

  8. Try:

    $username = $_POST['username'];
    $pdo = new PDO('mysql:host=xxxxx;dbname=xxxx', 'xxxx', 'xxxx');
    $sql = "SELECT COUNT(*) from TABLENAME where username='".$username."'";
    
    if ($result = $pdo->query($sql)
    {
        if ($result->fetchcolumn() > 0)
        {
            $sql = "SELECT * from TABLENAME where username='".$username."'";
            
            foreach ($pdo->query($sql) as $row)
            {
                echo $row['Player_Name'] . ' ' . $row['Time_Online'];
            }
        }
    }
    else 
    {
        echo "Sorry, that username does not exist in our database.";
    }
    
    
  9.  

     

    Sorry what i ment is .. If i enter username Alpha instead of Bravo.. it'll show Alpha's row's

    There is a problem in your logic. If you enter any value in the form that exists in the database - it's going to return that row's value because you asked it to with this select statement.

    SELECT * from TABLENAME where username='".$username."'"
    
  10. $countries[] = array($Ctry_info['CID'], $Ctry_info['Country']); 
    
    buildSelectOptions( $countries );
    
    function buildSelectOptions($options)
    {
    $optionsHTML = "<select name=\"countries\">\r\n";
    
    foreach($options as $c)
    {
    $optionsHTML .= "<option value=\"{$c[0]}\">$c[1]</option>\r\n";
    }
    $optionsHTML .= "</select>\r\n";
    
    return $optionsHTML;
    }   
    

    This code is meant to demonstrate how things could be done in your code.

    Wanted you to think, not just paste other's code as that does nothing for your knowledge. 

    Let me demonstrate how this code works and how it could be used - and there is several ways to do the same thing.

    This might confuse you further or help you understand what's going on.

    <?php
    
    // lets assume this is your DB result set
    $row = array(
    array('CID'=>100,'Country'=>'France'),
    array('CID'=>200,'Country'=>'Germany'),
    array('CID'=>200,'Country'=>'Germany')
    );
    
    // define the array to pass to buildSelectOptions() function
    $countries = array();
    
    // loop through the DB result set and extract information
    // with a db result set most of this work is done for you behind the scenes
    // but we will have to do the work, since we don't have a result set for demo purposes
    
    foreach ($row as $r)
    {
        $c = array();
    
        foreach ($r as $key => $value)
        {
            $c[] = $value;
        }
    
        // add the CID and Country of each row in the result set
        // to a row in the $countries array
        // in your result set this is all you would need
        // array($Ctry_info['CID'], $Ctry_info['Country']);
        // here we need to use the temp array $c
        $countries[] = array($c[0], $c[1]);
    }
    
    // in your html code, you can simply do this
    // call function and pass array as argument
    // echo buildSelectOptions( $countries );
    // for demo - use this code to see how it works
    echo htmlspecialchars(buildSelectOptions( $countries ));
    

    Here is the buildSelectOptions() function again.

    function buildSelectOptions($options)
    {
        $optionsHTML = "<select name=\"countries\">\r\n";
    
    foreach($options as $c)
    {
            $optionsHTML .= "<option value=\"{$c[0]}\">$c[1]</option>\r\n";
    }
    $optionsHTML .= "</select>\r\n";
    
    
    return $optionsHTML;
    
    }
    
  11.  

     

    That's exactly what i want. Use the variables ( user selections) to obtain an id from other table before sending the form and then send the user selection plus the obtained id to the db

     

    I believe someone already mentioned that will need to use Ajax for this. However, when you created the user select element - you must have gotten the id for that item in the db - you won't need Ajax if you just create the select with the id as the value.

    <select name="whatever">
    <option value="db_id">whatever name</option>
    </select>
    

    Use php to dynamically create the select element in the form - that's the whole purpose of the language.

  12. I was going to mention cutting out the middle man, but thought - maybe he uses it for other things, besides building a select array.

     

    But I was referring to this anomaly in the code:

    $countries = array(); 
    $Ctry_info = array(); 
    $Ctry_info['CID'] = 107;
    $Ctry_info['Country'] = 'Spain';
    print_r( $Ctry_info ); // key value array
    $countries[$Ctry_info['CID'] = $Ctry_info['Country']]; // error
    print_r( $countries );
    exit;
    
×
×
  • 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.