Show users username

It seems like right now you need some basic PHP knowledge. Try going to udemy.com and look for a cheap, introductory level, PHP course.

Instead of putting everything in an array , can I just select them one by one ? For example : select username from the users table , later select the news feed posts from the posts table , pictures from pictures table etc .

Also how do I block someone on here ?

Can you share some code?

It’s really t same as last time . 

auth_login.php:

if(!isset($\_SESSION['username'])){ echo"Download the app"; die(); }

profile.php:

include("auth\_login.php"); echo $\_SESSION['username']; 

profile.lua:

local userName local userNameText local userName = tostring(userName) display.newText( userName, display.contentCenterX, display.contentCenterY, native.systemFont, 20 ) local function networkListener( event ) if ( event.isError ) then local alert = native.showAlert( "Error Loading .", "Check your internet connection .", { "Try again" } ) end end function scene:create(event) local screenGroup = self.view local background = display.newImageRect("insta.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local userNameText = display.newText(userName, 200, 200, native.systemFont, 30 ) userNameText:setFillColor( 1, 0, 0 ) screenGroup:insert(userNameText) end

Per Forum Rules https://coronalabs.com/forum-rules/  #2 - Post only once, I’ve locked your other thread. The discussion needs to stay here. Also important is rule #13 - “Please limit bumping”.

While we want to limit bumping, it is preferable than creating a new post. Just understand that it takes time for the right people to see your post and you need to respect that. Therefore we ask that you give your threads a couple of days without a response before you ask for more help on the thread. This has the effect of bringing it back to the top of the recent posts lists (i.e. bumping it to the top).

These rules exist so that we have a strong community.

You asked “How can I block someone on here?”  You can go to “Your Settings” at the top of the page. From there click on ‘Ignore’ preferences. You can then add people to block. See:

In your other post, you said the answer offered here was too complex and then you posted code that may not really help anyone answer your question. 

Now looking at the code in the other post (which may have been useful to post here instead), in your network request listener, you are not sending any information to profile.lua.  You have these two lines of code:

composer.gotoScene("newsfeed")

That line of code will cause your “newsfeed.lua” scene to load. No information is being sent to it since you didn’t provide the extra parameters.

params = { username , score=currentScore }

This line of code creates a global variable named params that contains a table with an array-like element with the username variable and a key-value pair named score with the current score.  There are several issues with this. First it’s global. Secondly you shouldn’t mix key-value type tables and array-like index tables in the same table. However I honestly don’t believe that’s what you’re trying to do. What I think you wanted to do is pass that information to the newsfeed scene. In that case your composer.gotoScene should look like:

composer.gotoScene( "newsfeed", { params = { username = username, score = currentScore } } )

Now that still may not work because it makes a huge assumption that you have a variable named “username” that is scoped so this function can see it and that it has a value in it. You’ve not shown enough code to let us know if this will work. But assuming that username exists, it should work.  Note: There is additional work to do in newsfeed.lua.

One thing when you look at this is that you see username = username. The left side one is a key name in a table. The right one is the value to store in the keyname. It might be more helpful for you to learn this in a different way:

local parametersToSend = {} parametersToSend = username parametersToSend = currentScore composer.gotoScene( "newsfeed", { params = parametersToSend } )

Again you have to make sure at this point in your code “username” is a real variable that can be seen from this function (scope) or this won’t work.

In your other post you talk about profile.lua, which doesn’t look like a scene, but perhaps you only showed a small portion of it. Your code above is trying to go to newsfeed.lua. We need to see that. 

Generically speaking if you’re going to pass parameters this way, you have to do more work in the scene:create() of the scene you are going to:
 

function scene:create(event) local screenGroup = self.view local passedInParams = event.params --\<------ important. This is how you get the passed values local background = display.newImageRect("insta.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local userName = passedInParams.username --\<---- store the username in a local variable. local userNameText = display.newText(userName, 200, 200, native.systemFont, 30 ) userNameText:setFillColor( 1, 0, 0 ) screenGroup:insert(userNameText) end

This may seem to go contrary to previous advice about making userName local at the top. That was suggested under the assumption that you were getting the username from some source in *this scene*. Since it’s coming from a different scene, we have to take a different approach. 

Rob

I just got help on stack overflow . I did this :

login.lua :

local function networkListener( event ) if ( event.isError ) then local alert = native.showAlert( "Error Logging In", "Check your internet connection .", { "Try again" } ) else if event.response == "success" then -- put the code here to go to where the user needs to be -- after a successful registration composer.setVariable( "username", username.text ) composer.gotoScene( "profile", { params = parametersToSend } ) else -- put code here to notify the user of the problem, perhaps -- a native.alert() dialog that shows them the value of event.response -- and take them back to the registration screen to let them try again local json = require("json") print( json.prettify( event ) ) local alert = native.showAlert( "Error Logging In", event.response , { "Try again" } ) end end end

profile.lua :

-- at the top local userName = composer.getVariable( "username" ) function scene:create(event) local screenGroup = self.view local background = display.newImageRect("insta.jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local passedInParams = event.params --\<------ important. This is how you get the passed values local userNameText = display.newText(userName, 200, 200, native.systemFont, 30 ) userNameText:setFillColor( 1, 0, 0 ) screenGroup:insert(userNameText) end

and now I see the username .

In your composer.gotoScene() are you going to profile.lua or newsfeed.lua?

The error means your userName variable is still nil. You need to figure that out.  In the scene where you are calling composer.gotoScene() perhaps print out the value of username to make sure it’s set to a valid value. If it’s not you need to figure out why.

This sounds a whole lot like a “scope” problem in your scene where you’re calling composer.gotoScene(). Do you understand scope? (when variables are visible or not)?  It’s a really important concept (when to use local, when not to, how to get variables in one function available in others). You probably should read: https://docs.coronalabs.com/tutorial/basics/scope/index.html

To help you understand this, perhaps this example will illustrate:

local function networkListener( event ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print( username ) -- will be nil end local username = "abc123" network.request( ... )

This will fail. When Lua scans the code and comes to the network listener function, username hasn’t bee defined yet. Here’s another version:

local function networkListener( event ) print( username ) -- will be nil end local function logUserIn() local username = "abc123" network.request( ... ) end

This will result in the same issue, but “username” is more hidden since it’s local inside of a function. The solution is to make username local at a higher block before it gets used.

Consider this:
 

local username --\<---- now exists local function networkListener( event ) print( username ) -- will be nil end username = "abc123" --\<----- not local here, it's defined at the top, but it exists throughout the whole scene network.request( ... )

You really need to grasp this concept. It will solves one of the most common and frustrating problems that Lua developers run in to.

Rob

@Rob Miracle did you see my last comment here ? Is that a good way of doing it ? Because it works .

That’s a perfectly acceptable way to pass data between scenes.

Rob

Really nowhere near enough info in your question.

Assuming you’ve logged the user in and can get their user name from whatever social media provider you mean…

https://docs.coronalabs.com/api/library/display/newText.html

-- Assuming you put the user's name in the variable userName display.newText( userName, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )

I’m getting this error :

 local username = display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 ) screenGroup:insert(username) bad argument #1 to 'newText' (string expected, got nil)

Let me ask you to answer some basic questions.  (Please answer them all)

  1. What debug steps did you take on your own before asking your follow-up question above?

  2. What is argument #1?  That is, when you read that error message, what do you think argument #1 is refefring to?

  3. What did you put in the variable ‘userName’?

  4. Did you verify that there is something in ‘userName’?

  1. When I first got the error , I thought I was missing this ‘screenGroup:insert(username)’ so when I put that line I still got the error . I checked over and over that line of code and nothing changed so I posted here .
  2. What I think argument #1 is referring to is possibly a typo .
  3. This ‘local username’ is at the top of my code 
  4. I don’t think so

The Error Message

The error message is referring to this code:

display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )

Not any other line of code in that file.  In fact, I’m 99% sure you get a line number with the error message. 

Argument #1 is the first argument passed to the function display.newText()  i.e. The variable ‘userName’

That error message means …

 “Hey!  You were supposed to pass a string as argument #1 when calling diplay.newText(), but you didn’t.  You passed nil instead.

username versus userName

I noticed you used the spelling ‘username’ while I used the spelling ‘userName’. This is fine as long as your code is consistent.  Remember, Corona is case sensitive.

username Variable and Object?

I noticed you passed a varaible called ‘username’ to display.newText() and you created a new variable to store the display object in, called ‘username’.  This is just plain wrong…

Debugging This

First, you need to know how to read error messages.  Please see ‘The Error Message’ above.

Second, once you realize the the issue is Corona complaining about the variable ‘username’ you would debug this as follows:

print("my user name is", username) display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )

Now, run it and look at the console.  If it prints

my username is     nil

Then the variable ‘username’ has nothing in it.  That is wrong.

Try This Last

I MEAN IT!  DO THIS LAST!   Read everything above first or you’re wasting my time.   :frowning:

local userName = "comeUp264" local obj = display.newText userName, display.contentCenterX, display.contentCenterY, native.systemFont, 20)

I tried :

print("my user name is", username) display.newText( username, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )

and got the same error . I tried this :

local userName = "comeUp264" local obj = display.newText( userName, display.contentCenterX, display.contentCenterY, native.systemFont, 20 )

and got this error :

error loading module 'profile' from file 'C:\Users\user\Documents\Corona Projects\app\profile.lua': C:\Users\user\Documents\Corona Projects\app\profile.lua:29: unexpected symbol near '.' stack traceback: [C]: in function 'error' ?: in function 'gotoScene' C:\Users\user\Documents\Corona Projects\app\newsfeed.lua:42: in function '\_onPress' ?: in function '\_setSelected' ?: in function '?' ?: in function \<?:182\>

That is a problem with your code. 

Put my example in an empty main.lua file and it will work fine.

Other issues with your app are out of the scope of this discussion.

I didn’t expect your error to go away.  What did you get in the console?  Did you get the output I mentioned about a nil.

I can’t help more on this.

why should I empty main.lua ?

@Ed I have stopped helping this guy as he doesn’t listen or act on advice.  I suggest you do the same mate.

600+ posts and he can’t lookup the correct syntax for a newText ?