How do I use a list in combination with mysql

0 favourites
  • 6 posts
From the Asset Store
Over 2700 files with all the colour combinations from a 15 colours palette!
  • Hello,

    Let me begin with the famous words: "I'm not a coder "

    I'm wondering if somebody here knows the answer for my current issue:

    I would like the following:

    I have a list. This list contains names. When the user chooses, let's say, the name 'John Doe' construct 2 calls a PHP file and it echos the row that contains the name 'John Doe' and underlying tables like 'scores' etc.

    What I currently have in my 'get php file' is:

    $sql="SELECT * FROM scores ORDER BY score DESC LIMIT 100";
    $result=mysql_query($sql);
    
    // Start looping rows in mysql database.
    while($rows=mysql_fetch_array($result)){
    echo $rows['name'] . "|" . $rows['score'] . "|" . $rows['data1'] . "|" . $rows['data2'] . "|" . $rows['data3'] . "|" . $rows['data4'] . "|" ;[/code:3bsb2lxc]
    
    Construct is set up that It provides the right data in the app for calling the rows. Only for the person with the highest score. When I select a different name, nothing happens.
    
    Anybody any ideas on how to resolve this?
    
    Many thanks in advance!
    
    Kind regards
  • The first line in your php retrieves 100 top scores. If John Doe is not in the top 100, his score will not be shown.

    If you want to display first 100 plus John Doe, you need to first retrieve 100 top scores, and then get one or several highest scores for John Doe:

    SELECT * FROM scores WHERE name='John Doe' ORDER BY score DESC LIMIT 1[/code:vknipp8z]
    
    Or modify your SELECT statement, probably to something like this:
    [code:vknipp8z]SELECT....TOP 100 
    *UNION* 
    SELECT ... WHERE name='John Doe'...[/code:vknipp8z]
  • Thanks for your reply,

    I've already tried that and for just one name that's ok. In the list there are all different names. So this is a variable.

    How to set the code that when 'x' name is choosen, that line pops up from the database

    any ideas?

    kind regards

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You mean you need to pass a name from the list to php?

    I believe you do this by adding parameters to the URL, something like this:

    AJAX Request Url "https://myserver.com/GetScores.php?PlayerName=" & NamesList.SelectedText
    
    In PHP:
    
    $name=$_GET["PlayerName"];
    $sql="SELECT * FROM scores WHERE name='" . $name . "' LIMIT 3";
    ...
    [/code:3s3ngeez]
    
    I did this many years ago, so not sure if the syntax is correct.. 
    There may be issues with space character, maybe first try testing with one-word names.
    
    There are a few tutorials you can study:
    [url=https://www.scirra.com/tutorials/4839/creating-your-own-leaderboard-highscores-easy-and-free-php-mysql]https://www.scirra.com/tutorials/4839/c ... -php-mysql[/url]
    [url=https://www.scirra.com/tutorials/346/online-high-score-table-ajax-php-mysql]https://www.scirra.com/tutorials/346/on ... -php-mysql[/url]
  • What you say is correct: when person 'x' is choosen in a list, data of this person is shown. If person 'Y' is picked in the list, data from person 'Y' must be shown. I've tried your idea only the provided code doesn't seem to do the trick.

    I've seen the tutorials you mentioned. I've used one of them for the PHP part. Any other ideas on how to achieve what i want/need?

    Again tnx for the help!

  • I'm pretty sure this is the right way to pass data to php. Those tutorials I posted above use the same procedure to write scores to the database.

    So either your sql table has a different structure, or you made some mistake in php or C2 events.

    Does it work if you hard-code the name into SELECT statement? Do you get that player's scores in C2?

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)