The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/showthread.php 28 require_once





× This forum is read only. As of July 23, 2019, the UserSpice forums have been closed. To receive support, please join our Discord by clicking here. Thank you!

  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
$variable inside file_get_contents
#1
Hello!

I have recently started using UserSpice and I think it's awesome so far! I've never experienced working with a framework this uncomplicated. So cred to the developers, keep up te good work Smile

I have run into a slight problem, and I've been looking around on the web and on this forum, and in the documentation, but haven't found exactly what i'm looking for. Being inexperienced with PHP doesn't help either.

So I thought i'd give it a shot asking you guys Smile

Here goes:

I have a page, let's called it mypage.php

On that page, I have included a php file. Let's call it include1.php.

It is reloaded every second using a piece of javascript. Like this:

<pre>
Code:
<div class="View"></div>

<script>
setInterval(function(){
$.ajax({
            url: "include1.php",
            cache: false,
            success: function(html){        
                $(".View").html(html);          

            },
        });
},1000);
</script>
</pre>



That document has a list of other php files included. Let's call one of them include2.php.

It stems out like a tree, but my question is regarding what is suppose to happen inside include2.php.

The code in there looks like this:

<pre>
Code:
<?php
$my_url = 'http://awebsite.com/'.$user->data()->MyOwnColumn.'/other/stuff';

$test_var = file_get_contents($my_url, NULL, NULL, 5, 2);
echo $test_var;

if ($test_var === "Hi") {

echo '
Success!';

}
?>
</pre>



So the idea here is that include2.php will get text from a URL that is different for each user, depending on what is stored in "MyOwnColumn", that I have added to the database. There is only plain text on the URL which file_get_contents will read.

I have tried this:

<pre>
Code:
$my_url = 'http://awebsite.com/'.$user->data()->MyOwnColumn.'/other/stuff';
echo $my_url;
</pre>



And it works fine. I found "$user->data()->MyOwnColumn" in the UserSpice account.php file and tried it. I'm not sure if this is it's correct usage? I only altered the column to fit my own needs.

I have also tried the following, and it works fine with the piece of javascript reloading it:

<pre>
Code:
$test_var = file_get_contents('http://awebsite.com/in/plain/text', NULL, NULL, 5, 2);
echo $test_var;
</pre>



I have also tried this, but WITHOUT javascript, only
Code:
<?php include 'include1.php'; ?>

<pre>
Code:
$test_var = file_get_contents('http://awebsite.com/'.$user->data()->MyOwnColumn.'/other/stuff', NULL, NULL, 5, 2);
echo $test_var;
</pre>



And that works fine.

So the problem here seems to be, that when I use javascript to reload the included php files, it won't accept that I have put $my_url in the url which file_get_contents will "read".

The reason why I am asking here and not stackoverflow for example, is that I am not really sure how to correctly fetch data from the db in userspice. I know there is documentation on that, but I don't want to fetch everything in a table, but only from the logged in user. And that's where my knowledge of PHP limits me.

I hope I haven't been too confusing, but feel free to ask questions if I have Smile

Best regards!
`
  Reply
#2
When you are trying to query database, but you only want result from specific user, there is MySQL clause for that.

You can use WHERE clause.

Check WHERE clause for an example.

If you couldnt figure it out, paste your db query here so we can help you.
  Reply
#3
I agree with Jug...if you could do your query itself inside that second page, that would work. The other option is to do some sort of function in the usersc/includes/custom_functions.php

You can do something like

function getColumn($id){
//create function here
}

and then you call it by doing
getColumn($user->data()->id);

and the function would do the query to your row for you. Like JUG said...if you post the query you're trying to do, we can make it work.

Glad you're enjoying UserSpice!
  Reply
#4
Thank you for replying!

I should have added, init.php is of course included in "mypage.php".
The way I thought things worked, was that init took care of the mysql magic, and I could simply add:

Code:
"$user->data()->MyOwnColumn"


on any page where I wanted the information from that column to appear. I may very well have misunderstood how this works completely.

I'm afraid I haven't done a query at all I guess. I stole that piece of code from the "account.php" and simply changed the column name to fit my needs Smile I works until I put it inside the URL in file_get_contents in combination with a javascript reload.
  Reply
#5
Did you created new column in phpmyadmin, before you tried to query db?
  Reply
#6
Yes, I have added a column to the DB and it works like intended.

Let's say the column contains 1234567.

I can echo $user->data()->MyOwnColumn and it will correctly display "1234567".

I can also do the following:

Code:
$test_var = file_get_contents('http://awebsite.com/'.$user->data()->MyOwnColumn.'/other/stuff', NULL, NULL, 5, 2);
Code:
echo $test_var;

and that will correctly display "1234567"

I can reload the following using javascript:

Code:
$test_var = file_get_contents('http://awebsite.com/1234567/other/stuff', NULL, NULL, 5, 2); echo $test_var;

I can't reload the following using javascript:

Code:
$test_var = file_get_contents('http://awebsite.com/'.$user->data()->MyOwnColumn.'/other/stuff', NULL, NULL, 5, 2); echo $test_var;

Maybe it's a javascript limitation?

Thanks Smile
  Reply
#7
Yeah. Because javascript is a client side language, it does not have direct access to the database, although you can get a little sneaky.

So what you probably want to do is to write a function in javascript or php to make that happen.

I'm not a wizard but off the top of my head you would make a function like

function testVar($column){
//your test_var call with $column passed into the url
}

Then when you call the function it would be
testVar($user->data()->MyOwnColumn); in php
or something like
testVar(<?=$user->data()->MyOwnColumn?>); in js
Maybe with quotes around the php variable in javascript. I don't have my sample code with me right now.
  Reply
#8
Got it. Thanks for pointing me in the right direction.

Okay so I am trying to put the query itself inside the second page. I am encountering some weird stuff right now. I am trying to get the query you are showing in the DB class documentation video to work, and it simply won't. I am right now trying to get it to work on the main page (with init.php and header.php included of course)

I tried the most basic example:

Code:
<?php $users = $db->query("SELECT * FROM users"); $count = $users->count(); echo $count; ?>

This query isn't related to the function I want, but I can't even get that to work. Any idea why?
  Reply
#9
That is strange. What kind of error are you getting?
  Reply
#10
Sorry for the late response, been away for the weekend.

The error that I am getting is that the html is cut off right where I place the php. So when I right click on the page to view the source, no code is shown below where I have placed the php code. That's it.

I've looked through the document to see if there were any end tags missing, but there's not.

Edit: Actually, to be more specific:

The query itself doesn't seem to cut off the rest of the code. I tried placing it in the designated place for php:

`<?php
//PHP Goes Here!
$users = $db->query("SELECT * FROM users");
?>'

And then <?=$users?> further down in a <div>.

The rest of the code is cut off below the point where I have placed the echo. (And no result is shown from the query)
  Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)