Jump to content

Transforming a number to text


snallemap

Recommended Posts

Alright, so i've decided to just call some of the things in my MySQL for numbers because i decided that in the future i will turn that number into a text... Now i have been looking a bit at it, but im not quite sure how to do...

 

 

Im looking to create a code so that

 

if status is = 1

then status should appear as: member

 

if status is = 2

then status should appear as: donator

 

if status is = 3

then status should appear as: admin

 

if status if = 4

then status should appear as: super admin

 

Now the problem is just that, i keep making errors, so now im asking you for help.

 

- Status is a field in my user database

Link to comment
https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/
Share on other sites

Get a field from the database through a select statement:

 

http://www.w3schools.com/sql/sql_select.asp

 

And then use a case statement to get it to echo out the text you want, or to save a second variable to that specific text:

 

http://php.net/manual/en/control-structures.switch.php

<?php 
        include "config.php";
        
  $status = "SELECT status FROM gusers"

if ($status == 0) {
    echo "Member";
} elseif ($status == 1) {
    echo "Admin";
} elseif ($status == 2) {
    echo "Super Admin";
}

?>

 

It says there is an error on line 6.... Whats the problem with it :( Please help.

What's the error code?

 

(Are you reading ANY of our replies? I believe this is the 3rd thread of MULTIPLE people asking you for the error MESSAGE)

 

Yes i am.... Is it this error message you all want then?:

 

 

Parse error: parse error in C:\wamp\www\kawaii\statusref.php on line 6

<?php 
        include "config.php";
        
  $query = "SELECT status FROM gusers";
  $result = mysql_query($query)or trigger_error(mysql_error(), E_USER_ERROR);

  $status = array(1 => 'Member', 'Donator', 'Admin', 'Super Admin');
      while ($row = mysql_fetch_row($result)) :
         echo $status[$row[0]];
      endwhile;

?>

 

FYI, you missed a semicolon:

  $status = "SELECT status FROM gusers"

<?php
        include "config.php";
        include "statusref.php";

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT username, email, name, age, sex, status FROM gusers WHERE id = $id LIMIT 1";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "Character: " . $row['username'] . "\n<br>";
      echo "Email: " . $row['email'] . "\n<br>";
      echo "Real Name: " . $row['name'] . "\n<br>";
      echo "Age: " . $row['age'] . "\n<br>";
      echo "Sex: " . $row['sex'] . "\n<br>";
      echo "Rank: " . $row['status'] . "\n<br>";
}
  }
}

 

here we have my profile page.. It works just fine so don't think too much about that part right now.

 

 

Now to the actual problem

 

 <?php 
        include "config.php";
        
  $query = "SELECT status FROM gusers";
  $result = mysql_query($query)or trigger_error(mysql_error(), E_USER_ERROR);

  $status = array(1 => 'Member', 'Donator', 'Admin', 'Super Admin');
      while ($row = mysql_fetch_row($result)) :
         echo $status[$row[0]];
      endwhile;

?>

 

<?php 
        include "config.php";
     
  $status = "SELECT status FROM gusers";

if ($status == 0) {
    echo "Member";
} elseif ($status == 1) {
    echo "Admin";
} elseif ($status == 2) {
    echo "Super Admin";
}

?>

 

Now these codes are supposed to make the status (RANK) appear as text instead the actual number. The code were put into statusref.php which i later included to the profile page, now the text appears... The only problem is, it dosen't get the rank from the profile page that i am on, and it dosen't appear in the right spot where it were supposed to

 

 

The first code makes this appear:

 

Notice: Undefined index: 0 in C:\wamp\www\kawaii\statusref.php on line 9

 

Notice: Undefined index: 0 in C:\wamp\www\kawaii\statusref.php on line 9

 

Notice: Undefined index: 0 in C:\wamp\www\kawaii\statusref.php on line 9

Super AdminCharacter: test

Email: [email protected]

Real Name: Simon

Age: 18

Sex: 1

Rank: 4

 

You get Super Admin nomatter what profile you go to, and the superadmin stands in same line as Character ... it were supposed to replace the 4 in Rank:

 

This is what happens with the other script:

 

MemberCharacter: test

Email: [email protected]

Real Name: Simon

Age: 18

Sex: 1

Rank: 4

 

That appears as member, again same problem with position and loading from the actual profile...

 

 

HELP WOULD BE SOO APPRICIATED :(

 

<?php
        include "config.php"; //<--add the below line to config.php
        $statusArray = array(1 => 'Member', 'Donator', 'Admin', 'Super Admin');
       //forget statusref// include "statusref.php";

if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT username, email, name, age, sex, status FROM gusers WHERE id = $id LIMIT 1";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "Character: " . $row['username'] . "\n<br>";
      echo "Email: " . $row['email'] . "\n<br>";
      echo "Real Name: " . $row['name'] . "\n<br>";
      echo "Age: " . $row['age'] . "\n<br>";
      echo "Sex: " . $row['sex'] . "\n<br>";
      echo "Rank: " . $statusArray[$row['status']] . "\n<br>";
}
  }
}

 

Whenever you pull status from the users table, to display it as text, use the row pulled as the key in $statusArray.

Hi

 

Think you are getting confused with the 2 scripts (or I am)

 

Try like this

 

        include "config.php";
        include "statusref.php";
$status = array(1 => 'Member', 'Donator', 'Admin', 'Super Admin');
if (isset($_GET['id'])) {
  $id = mysql_real_escape_string($_GET['id']);
  $sql = "SELECT username, email, name, age, sex, status FROM gusers WHERE id = $id LIMIT 1";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      $row = mysql_fetch_assoc($result);
      echo "Character: " . $row['username'] . "\n<br>";
      echo "Email: " . $row['email'] . "\n<br>";
      echo "Real Name: " . $row['name'] . "\n<br>";
      echo "Age: " . $row['age'] . "\n<br>";
      echo "Sex: " . $row['sex'] . "\n<br>";
      echo "Rank: " . $status [$row['status']] . "\n<br>";
}
  }
}

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.