snallemap Posted September 23, 2010 Share Posted September 23, 2010 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 More sharing options...
kenrbnsn Posted September 23, 2010 Share Posted September 23, 2010 Please post your code and any error messages you're getting. Ken Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114612 Share on other sites More sharing options...
snallemap Posted September 23, 2010 Author Share Posted September 23, 2010 I don't have a code since i couldn't figure how to set it up when you gotta get the database info and then turn that into a text .... I were hoping you could tell me how to do Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114614 Share on other sites More sharing options...
Miss_Rebelx Posted September 23, 2010 Share Posted September 23, 2010 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 Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114615 Share on other sites More sharing options...
Andy-H Posted September 23, 2010 Share Posted September 23, 2010 //query user table, pull `status` field $status = array(1 => 'Member', 'Donator', 'Admin', 'Super Admin'); echo $status[$arr['status']]; Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114616 Share on other sites More sharing options...
snallemap Posted September 23, 2010 Author Share Posted September 23, 2010 <?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. Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114621 Share on other sites More sharing options...
snallemap Posted September 23, 2010 Author Share Posted September 23, 2010 //query user table, pull `status` field $status = array(1 => 'Member', 'Donator', 'Admin', 'Super Admin'); echo $status[$arr['status']]; This didn't seem to work for me Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114623 Share on other sites More sharing options...
Miss_Rebelx Posted September 23, 2010 Share Posted September 23, 2010 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) Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114624 Share on other sites More sharing options...
snallemap Posted September 23, 2010 Author Share Posted September 23, 2010 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 Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114626 Share on other sites More sharing options...
Miss_Rebelx Posted September 23, 2010 Share Posted September 23, 2010 Yes that's the error message we all want to see. I think the error is because you're comparing a number with a string. $status is a string, and 1 is a number. Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114629 Share on other sites More sharing options...
Andy-H Posted September 23, 2010 Share Posted September 23, 2010 <?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" Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114631 Share on other sites More sharing options...
snallemap Posted September 23, 2010 Author Share Posted September 23, 2010 <?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 Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114642 Share on other sites More sharing options...
Andy-H Posted September 23, 2010 Share Posted September 23, 2010 <?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. Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114646 Share on other sites More sharing options...
kickstart Posted September 23, 2010 Share Posted September 23, 2010 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 Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114647 Share on other sites More sharing options...
snallemap Posted September 23, 2010 Author Share Posted September 23, 2010 Thank you alot! It works like a charm ! Its perfect! Link to comment https://forums.phpfreaks.com/topic/214211-transforming-a-number-to-text/#findComment-1114654 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.