open url from sql query

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
User avatar
D Oliveira
Veteran Member
Posts: 76
Joined: 2018-03-04 09:30

open url from sql query

Post by D Oliveira » 2019-07-24 22:40

hello everyone, i put this code in my home page as an attempt to open an url based off a query from the db, please help if you can. thank you.

home.php

Code: Select all

<div class="row custom_links" id="custom_links">

<button type="button" class="btn btn-success btn-lg" style="width:40%" onclick="window.location.replace('coachrubens/../hooks/guidetotreinocardio_pdf.php')" >
<i class="glyphicon glyphicon-indent-right"></i> Treino e Cardio</button>

</div>
guidetotreinocardio_pdf.php

Code: Select all

<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");

	$mi = getMemberInfo();

	$user = $mi['username'];


	$id = sqlValue('SELECT MAX(ordem) FROM treinocardio WHERE user = $user');

	$id2 = sqlValue('SELECT id FROM treinocardio WHERE ordem = $id');

			window.open('treinocardio_pdf.php?id=' + $id2);

User avatar
D Oliveira
Veteran Member
Posts: 76
Joined: 2018-03-04 09:30

Re: open url from sql query

Post by D Oliveira » 2019-07-24 22:54

updated guidetotreinocadio_pdf.php, still not working

Code: Select all


<?php
	$currDir = dirname(__FILE__) . '/..';
	include("$currDir/defaultLang.php");
	include("$currDir/language.php");
	include("$currDir/lib.php");

	$mi = getMemberInfo();

	$user = $mi['username'];

	$max_ordem = sqlValue('SELECT MAX(ordem) FROM treinocardio WHERE user = $user');
	$id = sqlValue('SELECT id FROM treinocardio WHERE ordem = $max_ordem AND user = $user');


        window.open('treinocardio_pdf.php?id=' + $id);

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 218
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: open url from sql query

Post by jsetzer » 2019-07-26 07:48

You are mixing up JavaScript with PHP
  1. window.open belongs to JavaScript. In PHP you can use the header-function: https://www.php.net/manual/en/function.header.php

    Code: Select all

    header('Location: http://www.example.com/');
    
  2. string-concatenation in 'treinocardio_pdf.php?id=' + $id
    The "+" can be used to concat strings in JavaScript. In PHP you have to use "."

    Code: Select all

    $url = 'treinocardio_pdf.php?id=' . $id;
    
SQL-Syntax
  1. missing quote
    In your sql-statement there is are single quotes missing around $user variable because this is a string. Try this instead:

    Code: Select all

    $id = sqlValue('SELECT `id` FROM `treinocardio` WHERE `ordem` = {$max_ordem} AND `user` = '$user' LIMIT 1');
    
Please retry after these changes.

Regards,
Jan

User avatar
D Oliveira
Veteran Member
Posts: 76
Joined: 2018-03-04 09:30

Re: open url from sql query

Post by D Oliveira » 2019-07-27 04:30

jsetzer wrote:
2019-07-26 07:48
You are mixing up JavaScript with PHP
  1. window.open belongs to JavaScript. In PHP you can use the header-function: https://www.php.net/manual/en/function.header.php

    Code: Select all

    header('Location: http://www.example.com/');
    
  2. string-concatenation in 'treinocardio_pdf.php?id=' + $id
    The "+" can be used to concat strings in JavaScript. In PHP you have to use "."

    Code: Select all

    $url = 'treinocardio_pdf.php?id=' . $id;
    
SQL-Syntax
  1. missing quote
    In your sql-statement there is are single quotes missing around $user variable because this is a string. Try this instead:

    Code: Select all

    $id = sqlValue('SELECT `id` FROM `treinocardio` WHERE `ordem` = {$max_ordem} AND `user` = '$user' LIMIT 1');
    
Please retry after these changes.

Regards,
Jan
Thank you for taking the time to construct such an answer, code worked perfectly, I keep catching myself with those minor syntax details, guess since I've came from .lua my brain always has a hard time differentiating all the pertaining operators and parameters.

Cheers

David Oliveira

Post Reply