Need backup on PHP =(((

Get help using Construct 2

Post » Tue Mar 08, 2016 11:03 am

Message: Axiomaltd can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Friends, who'll tell me , how to load data from php array into the project? I mean, i have a php script that generates a data array , and now how an array via AJAX i can load into the project? dictionary-> asJSON-> AJAX.LastData or Array.JSON-> AJAX.Last data? I always get zeros. The idea is to create a turn-based multiplayer. Now i'm focused on how to set up the room. and it's not working =( all room in php database returnigng via AJAX.LastData as one big text, no separates =( Please, help me, how to setup php and c2-events correct?

lik to php code ---> https://cloud.mail.ru/public/9eg3/XRcbx9aHC
The whole life is in the movement.Stopped - fell asleep to hell...
B
14
S
3
G
1
Posts: 122
Reputation: 1,694

Post » Tue Mar 08, 2016 1:27 pm

UP
The whole life is in the movement.Stopped - fell asleep to hell...
B
14
S
3
G
1
Posts: 122
Reputation: 1,694

Post » Tue Mar 08, 2016 1:55 pm

Axiomaltd wrote:UP

@Axiomaltd

Please don't bump after only 2 hours. It is acceptable to bump your own thread after 24 hours if no answer has been forthcoming.

Be aware that those users who generally help come from varying time zones, so please have a little patience.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
134
S
30
G
86
Posts: 5,420
Reputation: 59,526

Post » Tue Mar 08, 2016 2:21 pm

If you check the How do I FAQ in the AJAX category you will see some example of script and AJAX communication you can take example on.
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
292
S
115
G
96
Posts: 7,295
Reputation: 70,795

Post » Tue Mar 08, 2016 2:53 pm

i'm already seen them. but the problem is - how separate data, that c2 recieves via ajax, how to correctly put it to the array, and how LIST can create seperate item for each data in array?
The whole life is in the movement.Stopped - fell asleep to hell...
B
14
S
3
G
1
Posts: 122
Reputation: 1,694

Post » Tue Mar 08, 2016 3:50 pm

Here are some of my old functions you might find helpful.

Code: Select all
protected function outputAsC2DisctionaryJSON($data)
   {
      $data = array(
         'c2dictionary' => true,
         'data' => $data
      );
      $this->outputData($data);
   }
   
   protected function outputAsC2ArrayJSON($data, $arrayDimensions)
   {
      $data = array(
         'c2array' => true,
         'size' => $arrayDimensions,
         'data' => $data
      );
      $this->outputData($data);
   }
   
   private function outputData($data)
   {
      header('Content-Type: application/json');
      echo json_encode($data);
      exit(1);
   }
   
   protected function outputSimpleData($data)
   {
      echo($data);
      exit(1);
   }


C2 has specific JSON format which these functions covers. As "data" you just provide an array.
In C2 you simply load dictionary with the AJAX.LastData.

Also remember that you need to set the PHP to accept crossdomain requests if you want to do it for mobile or so.
ImageImageImage
B
27
S
16
G
80
Posts: 1,020
Reputation: 44,759

Post » Tue Mar 08, 2016 4:26 pm

How do I FAQ wrote:AJAX

How do I pull variables out of a PHP page using AJAX ? - LINK
More about using PHP - LINK
Various questions and answers on using AJAX with server-side scripting to make an online game - LINK


Arrays

Getting values from an array - LINK
String to array example - LINK
Filling an array with a string and tokens - LINK
Concrete commented example of use of arrays - LINK
Sorting an array - LINK
Simple explanations on what is, how and why use arrays - LINK
Another thread about using arrays - LINK
Flood fill in an array - LINK
Video explaining how to fill an array - LINK
Fill a list from an array - LINK

List

Fill a list from an array - LINK
Change list items by selection in another list - LINK


Arrays, AJAX, List
New to Construct ? Where to start

Image Image

Image Image

Please attach a capx to any help request or bug report !
Moderator
B
292
S
115
G
96
Posts: 7,295
Reputation: 70,795

Post » Tue Mar 08, 2016 11:09 pm

thanx man ) really helps!
The whole life is in the movement.Stopped - fell asleep to hell...
B
14
S
3
G
1
Posts: 122
Reputation: 1,694

Post » Wed Mar 09, 2016 1:51 am

but here one little thing===>>>

code for posting to php:
<?php
//Записываем в переменные данные для доступа к БД

$mysql_host = "localhost";
$mysql_database = "123123123";
$mysql_user = "123123123";
$mysql_password = "123123123";

$link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("Ошибочка вышла...");
mysql_select_db($mysql_database, $link) or die ("Ой...");//Подключились к базе

@header('Access-Control-Allow-Origin: *');
@header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
@header("Content-Type: text/html; charset=UTF-8");


if (isset($_POST['login'])) { $login = $_POST['login']; if ($login == '') { unset($login);} }
if (isset($_POST['pass'])) { $pass=$_POST['pass']; if ($pass =='') { unset($pass);} }
if (isset($_POST['data'])) { $data=$_POST['data']; if ($data =='') { unset($data);} }
if (empty($login) or empty($pass) or empty($data))
{
echo ("Вы ввели не всю информацию, заполните все поля!");
exit();
}
$login = stripslashes($login);
$login = htmlspecialchars($login);
$pass = stripslashes($pass);
$pass = htmlspecialchars($pass);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$login = trim($login);
$pass = trim($pass);
$data = trim($data);
$q1 = mysql_query("SELECT login FROM `data` WHERE `login`='".$login."'");
$loginb = mysql_fetch_array($q1);
$loginbd=$loginb['login'];
if($loginbd == $login){
echo("Извините, введённый вами логин уже зарегистрирован.");
exit();
}
else
$result2 = mysql_query ("INSERT INTO data (login,pass,data) VALUES('$login','$pass','$data')");
if ($result2=='TRUE')
{
echo "Вы успешно зарегистрированы!";
}
else {
echo "Ошибка! Вы не зарегистрированы.";

}
?>




and the code to retrive data to c2dictionary:

<?php
//Записываем в переменные данные для доступа к БД

$mysql_host = "localhost";
$mysql_database = "123123123";
$mysql_user = "123123123";
$mysql_password = "123123123";

$link = mysql_connect($mysql_host, $mysql_user, $mysql_password) or die("Ошибочка вышла...");
mysql_select_db($mysql_database, $link) or die ("Ой...");//Подключились к базе

@header('Access-Control-Allow-Origin: *');
@header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
@header("Content-Type: text/html; charset=UTF-8");


//Проверяем наличие передеваемых данных
if (isset($_POST['loglog'])) { $login = $_POST['loglog']; if ($login == '') { unset($login);} }
if (isset($_POST['passwordlog'])) { $pass=$_POST['passwordlog']; if ($pass =='') { unset($pass);} }
if (empty($login) or empty($pass))
{
echo ("Вы ввели не всю информацию, заполните все поля!");
exit();
}
$login = stripslashes($login);
$login = htmlspecialchars($login);
$pass = stripslashes($pass);
$pass = htmlspecialchars($pass);
$data = stripslashes($data);
$data = htmlspecialchars($data);
$login = trim($login);
$pass = trim($pass);
$data = trim($data);

//Проверяем наличие полученных значений
if(isset($login) && isset($pass)){

//Запрос к БД на получение нужной строки
$q1 = mysql_query("SELECT login FROM `data` WHERE `login`='".$login."' AND `pass`='".$pass."'");
$loginb = mysql_fetch_array($q1);
$loginbd=$loginb['login'];
$q2 = mysql_query("SELECT pass FROM `data` WHERE `login`='".$login."' AND `pass`='".$pass."'");
$passwordb = mysql_fetch_array($q2);
$passwordbd=$passwordb['pass'];
if($passwordbd == $pass){
$r1 = mysql_query("SELECT data FROM `data` WHERE `login`='".$login."' AND `pass`='".$pass."'");

$row = mysql_fetch_array($r1); // БД в массив

$arr = array('c2dictionary' => 'true',
'data' => array('vin' => $row['vin'], 'year' => $row['year'],'mod' => $row['mod'],'phone' => $row['phone'],'fio' => $row['fio'],'mark' => $row['mark'])); // Каждое значение вводим вручную

echo json_encode($arr);
}

//если не верный пароль то покажем 1qqqq
else{
echo 'И кого это ты наебать хочешь???';
}
}

?>


and here is the question - where i'm mistayken? no data recieve at all =(((( :oops: :oops: :oops: :oops: :?: :?: :?:
The whole life is in the movement.Stopped - fell asleep to hell...
B
14
S
3
G
1
Posts: 122
Reputation: 1,694

Post » Wed Mar 09, 2016 2:45 am

Just what I've noticed at first sight 'c2dictionary' => 'true', it should be 'c2dictionary' => true. I'm not sure if it's a mistake, it might work if C2 checks with == but will fail if it checks with ===.

Anyway First of all I suggest to keep database credentials in some separate file. Also using some ORM DB library will give you better protection from mysql injection and your code will look much more tidy if you use some separate models for business operations, retrieving data etc.

You also did not set header to JSON application. If you just send JSON, you should do it.

Now what is exactly not working? Have you checked if AJAX.LastData has any content or you've checked the dictionary content after AJAX load?

Is there a problem with connecting to PHP?
Retrieving any data?
Filling dictionary?
PHP gives any error?

Try to provide as much info as possible whenever you ask for help.
ImageImageImage
B
27
S
16
G
80
Posts: 1,020
Reputation: 44,759

Next

Return to How do I....?

Who is online

Users browsing this forum: No registered users and 23 guests