Hiya DaniWeb members,

Before starting it's not basic $variables I need help with.

I've been working on a project lately for a Minecraft Server website and this project will work alongside the MCMMO plugin.

Most of it works but I would like some help/advise on how to turn

$nextLevel = $playerSkills[$playerSkillsKeys[$i]]."Level" + 1;

so when this is executed it will currently be dislayed as <text>Level

I would like to have it be turned into $<text>Level and then echo out the value of $<text>Level

Please see the code snippit below for a overview.

Thank you.

    <?php
    include_once "config.php";
    $player = "KingGold171";

    //get a player's ID from their name
    $sql_getPlayerIdFromName = "SELECT id FROM  mcmmo_users WHERE user LIKE '$player'";
    $r_getPlayerIdFromName   = mysqli_query($con, $sql_getPlayerIdFromName);

    while ($row = mysqli_fetch_object($r_getPlayerIdFromName))
    {
    $playerID = $row->id;
    }

    echo $playerID ;

    //get a player's skills form their ID
    $sql_getPlayerStats = "SELECT acrobatics,alchemy,archery,axes,excavation,fishing,herbalism,mining,repair,swords,taming,unarmed,woodcutting FROM mcmmo_skills WHERE user_id = $playerID;";
    $r_getPlayerStats = mysqli_query($con, $sql_getPlayerStats);

    while ($row = mysqli_fetch_object($r_getPlayerStats))
    {
    $playerSkills = (array) $row;
    }

    $playerSkills = array_slice($playerSkills, 0);


    $skillNames     = array(
        "acrobatics",
        "alchemy",
        "archery",
        "axes",
        "excavation",
        "fishing",
        "herbalism",
        "mining",
        "repair",
        "swords",
        "taming",
        "unarmed",
        "woodcutting",

    );


    $playerSkillsKeys = array_keys($playerSkills);
    $SkillNamesKeys = array_keys($skillNames) ;


    $query = mysqli_query($con, "SELECT mcmmo_users.user, mcmmo_skills.taming, mcmmo_skills.mining, mcmmo_skills.woodcutting, mcmmo_skills.repair, mcmmo_skills.unarmed, mcmmo_skills.alchemy, mcmmo_skills.herbalism, mcmmo_skills.excavation, mcmmo_skills.archery, mcmmo_skills.swords, mcmmo_skills.axes, mcmmo_skills.acrobatics, mcmmo_skills.fishing FROM mcmmo_users INNER JOIN mcmmo_skills ON mcmmo_users.id=mcmmo_skills.user_id WHERE mcmmo_users.user='$player' ");
    $num_url = mysqli_num_rows($query);
    while ($obj = mysqli_fetch_object($query)) {
        $acrobaticsLevel = $obj->acrobatics;
        $alchemyLevel = $obj->alchemy;
        $archeryLevel = $obj->archery;
        $axesLevel = $obj->axes;
        $excavationLevel = $obj->excavation;
        $fishingLevel = $obj->fishing;
        $herbalismLevel = $obj->herbalism;
        $miningLevel = $obj->mining;
        $repairLevel = $obj->repair;
        $swordsLevel = $obj->swords;
        $tamingLevel = $obj->taming;
        $unarmedLevel = $obj->unarmed;
        $woodcuttingLevel = $obj->woodcutting;      
    }

    $query2 = mysqli_query($con, "SELECT mcmmo_users.user, mcmmo_experience.taming, mcmmo_experience.mining, mcmmo_experience.woodcutting, mcmmo_experience.repair, mcmmo_experience.unarmed, mcmmo_experience.alchemy, mcmmo_experience.herbalism, mcmmo_experience.excavation, mcmmo_experience.archery, mcmmo_experience.swords, mcmmo_experience.axes, mcmmo_experience.acrobatics, mcmmo_experience.fishing FROM mcmmo_users INNER JOIN mcmmo_experience ON mcmmo_users.id=mcmmo_experience.user_id WHERE mcmmo_users.user='$player' ");
    $num_url = mysqli_num_rows($query2);
    while ($obj = mysqli_fetch_object($query2)) {
        $acrobaticsExp = $obj->acrobatics;
        $alchemyExp = $obj->alchemy;
        $archeryExp = $obj->archery;
        $axesExp = $obj->axes;
        $excavationExp = $obj->excavation;
        $fishingExp = $obj->fishing;
        $herbalismExp = $obj->herbalism;
        $miningExp = $obj->mining;
        $repairExp = $obj->repair;
        $swordsExp = $obj->swords;
        $tamingExp = $obj->taming;
        $unarmedExp = $obj->unarmed;
        $woodcuttingExp = $obj->woodcutting;
    }


for($i = 0; $i < sizeof($playerSkills); $i++){

    //just to make it a bit clearer
    echo "<br />" . $skillNames[$skillNamesKeys[$i]] ;

    $nextLevel = $playerSkills[$playerSkillsKeys[$i]]."Level" + 1;
    echo "<br />" . $nextLevel ;
    $getExp = mysqli_query($con, "SELECT total_exp FROM mcmmo_levels WHERE id = '$nextLevel';");
    $result = mysqli_num_rows($getExp);
    while ($obj = mysqli_fetch_object($getExp)) {
        $playerSkills."TotalExp" == $obj->total_exp;
    }

if ($nextLevel >=1001)
    {
    echo "The MCMMO cap's at 1000";
}
else
    {
$percent = $skillNames[$SkillNamesKeys[$i]]."Exp"  / $skillNames[$SkillNamesKeys[$i]]."TotalExp"; 
$percent1 = $percent * 100;

//===DEBUG===
echo "<br />Required exp:" . $skillNames[$SkillNamesKeys[$i]]  ."TotalExp";
echo "<br />Current exp:"  . $$skillNames[$SkillNamesKeys[$i]] ."Exp";
echo "<br />current / required:" . $percent;
echo "<br />* 100:" . $percent1;
//=== END DEBUG===

$arr1 = str_split($percent1);
$pb =  $arr1[0] . $arr1[1] . $arr1[2] . $arr1[3];

echo "<div id='page-wrap'>";
// $skillName = ucfirst($SkillNamesKeys[$i]); //ucfirst() = first letter uppercase

echo "<h3> Current " . $skillNames[$SkillNamesKeys[$i]] . " Level: " . $playerSkills[$playerSkillsKeys[$i]] .  "</h3>" ;
echo "<h2> Next " . $skillNames[$SkillNamesKeys[$i]] . " Level: " . $nextLevel.  "</h2>" ;

echo "<div class='meter'>";
echo "<span style='width:" . $pb . "%>";         
echo "<p>";
echo "<strong>";
echo "<span style='color:green'>";
echo $playerSkills[$playerSkillsKeys[$i]]."Exp" ;
echo "</span>";
echo "</strong>";
echo "</p>";         
echo "</span>";
echo "</div>";   
}

}
    ?>

Recommended Answers

All 12 Replies

The code is quite messy though I'd be interesting in seeing more.
Could you share your database structure so we can see datatypes etc?

Hiya matrixdevuk,

The code itself may not look neat, but it does work (apart from what I am here asking for assistance with), and im not sure how knowing the datatypes would help solve it othewise plus as stated in the beginning post it links onto a Minecraft Server Plugin and it's corrisponding database.

To clear up what im trying to achieve with the variables.

$percent = $skillNames[$SkillNamesKeys[$i]]."Exp" / $skillNames[$SkillNamesKeys[$i]]."TotalExp";
$percent1 = $percent * 100;

So with the above code snippit, it will get its name from the loop, and will effectivly have the same variable as the 2 SQL Querys below, but currently the output is only displaying plaintext whereas is would like it to be generated at it is doing, but display the contents of the variable.

$nextLevel = $playerSkills[$playerSkillsKeys[$i]]."Level" + 1;

Effeftively the output will be each of the following below, but currently returns plaintext.

 while ($obj = mysqli_fetch_object($query)) {
$acrobaticsLevel = $obj->acrobatics;
$alchemyLevel = $obj->alchemy;
$archeryLevel = $obj->archery;
$axesLevel = $obj->axes;
$excavationLevel = $obj->excavation;
$fishingLevel = $obj->fishing;
$herbalismLevel = $obj->herbalism;
$miningLevel = $obj->mining;
$repairLevel = $obj->repair;
$swordsLevel = $obj->swords;
$tamingLevel = $obj->taming;
$unarmedLevel = $obj->unarmed;
$woodcuttingLevel = $obj->woodcutting;
}

and the same happens here,

$skillNames[$SkillNamesKeys[$i]]."Exp"

 while ($obj = mysqli_fetch_object($query2)) {
$acrobaticsExp = $obj->acrobatics;
$alchemyExp = $obj->alchemy;
$archeryExp = $obj->archery;
$axesExp = $obj->axes;
$excavationExp = $obj->excavation;
$fishingExp = $obj->fishing;
$herbalismExp = $obj->herbalism;
$miningExp = $obj->mining;
$repairExp = $obj->repair;
$swordsExp = $obj->swords;
$tamingExp = $obj->taming;
$unarmedExp = $obj->unarmed;
$woodcuttingExp = $obj->woodcutting;
}

Image of the output: http://screencast.com/t/f68AG29u1t

I not totally understand the question though but if I not mistaken, your problem is changing the string to variable name in php? If it is, it is posible to be done by refering Click Here

commented: Helpful refrence guide +1

Hiya Ips,

As a clearer example,

$helloworld = "hello world";

Rather then just going

echo $helloworld;

You are are right about joining a string to the end of a variable

$variable = "hello"; 
echo $variable . "world";

effectively having

echo "$" . $variable . "world";

will work the same as

echo $helloworld;
Member Avatar for diafol

I don't understand how you're doing arithmetic on strings.

$percent = $skillNames[$SkillNamesKeys[$i]]."Exp" / $skillNames[$SkillNamesKeys[$i]]."TotalExp";
$percent1 = $percent * 100;

What output do you expect from that??

Diafol,

From my post before your responce you will see im after having it work like dynamic variables, thus when it comes to the arithmetic it will use the vaules of the dynamic variable.

if i could get the concept of dynamic variables to work.

Member Avatar for diafol

AH! Ok got you now. DoH! OK, like this perhaps:

$myvar = "unbelievable";
$varname = "myvar";

echo $$varname; //unbelievable
commented: Useful example +1
Member Avatar for diafol

SO maybe:

$percent = $$skillNames[$SkillNamesKeys[$i]]."Exp" / $$skillNames[$SkillNamesKeys[$i]]."TotalExp";
$percent1 = $percent * 100;

Dunno, can't really follow the code supplied, sorry.

//EDIT

Just noticed this is what lps linked to in the manual.

I think I can sort of see what you're asking, you mean do something like this? (ref)

$type = '123';
$name_123 = 'hello';

echo ${"name_$type"} . "\n";

// prints "hello"

When @MDUK says "quite messy", he really wasn't joking. Thats a complete mangled mush, and very awkward to work with. I cannot stress enough how important it is to look into basic OOP for PHP - just because it's optional does NOT mean it should be forgotten/omitted. It's a great way to neaten your code and speed up your app, especially since you'll have hungry gamers really testing it's performance using it hard on your Mincraft server.

At least that way you can get rid of the massive (and completely unnecessary) while loops. Also note, lines 125-135 can seriously be cleaned up - and don't forget to close attributes (i.e. line 126 echo "<span style='width:" . $pb . "%>"; should be echo "<span style='width:" . $pb . "%'>";).

[SOLVED]

Well after reading a lot more in depth on dynamic variables and a lot of code tweaks, currently I am starting to see a working end result as to what I am looking for.

$skills = $skillNames[$i];

$totalExp = 'TotalExp';
$exp = "Exp";

$displayExp = ${$skills . $exp};

echo "<br / >" . $displayExp;

Thank you all for your input and guidence.

~KingGold171

In a nutshell, by taking each item from this array,

$skillNames     = array(
    "acrobatics",
    "alchemy",
    "archery",
    "axes",
    "excavation",
    "fishing",
    "herbalism",
    "mining",
    "repair",
    "swords",
    "taming",
    "unarmed",
    "woodcutting",
);

and adding "Exp" to the end, by Which using the dynamic variable is now beginning to refrence to,

    while ($obj = mysqli_fetch_object($query2)) {
        $acrobaticsExp = $obj->acrobatics;
        $alchemyExp = $obj->alchemy;
        $archeryExp = $obj->archery;
        $axesExp = $obj->axes;
        $excavationExp = $obj->excavation;
        $fishingExp = $obj->fishing;
        $herbalismExp = $obj->herbalism;
        $miningExp = $obj->mining;
        $repairExp = $obj->repair;
        $swordsExp = $obj->swords;
        $tamingExp = $obj->taming;
        $unarmedExp = $obj->unarmed;
        $woodcuttingExp = $obj->woodcutting;
    }
Member Avatar for diafol

But you still need to know the names in your while loop. Can.t see why you dont use an array

A db results array that you could use to instantiate a real object. The only advanced I see here is a variables mess

commented: agreed +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.