Custom link (in List view settings tab?s element) from rowid --> Article id --> sql Query {source}

jmdc

Member
Good night,

I will try to explain my problem the best i can...

In my list view, i have a custom link in ID element wich redirect to an article: "index.php/component/content/article?id=4"

In this article i have a SOURCE php code which is:

{source}
<?php
$servername = "blabla";
$username = "blabla";
$password = "blabla";
$dbname = "blabla";

$con=mysqli_connect($servername, $username, $password, $dbname);
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_set_charset($con, "utf8");

$myrowid = '{rowid}';

$sql="SELECT ... WHERE id = '$myrowid' ";
$result=mysqli_query($con,$sql);
$row=mysqli_fetch_array($result,MYSQLI_NUM);
printf ("%s (%s)\n",$row[0],$row[1]);
?>
{/source}

My problem it?s the Where Condition in sql query....How can i tell that query that id is the first ID in list view selected?

If i click in my list view ID=41, i go to article ID=4 and in it, the where condition should be "Where id = '41'"

Could you help me,please?Or is there any other solutions for this?

Thank you in advance.
 
In the custom link you can include the listID with {listid}
index.php/component/content/article?id=4&mylist={listid}
 
I appreciate your answer, but what i am looking for is, i think, a $_GET process recovering id=41 from URL, in the SQL query in WHERE condition like:
SELECT ... WHERE id = '$_GET(???)'.

I hope you have understand my question.

Thank you.

______EDITED POST________
I have the solution in WHERE condition i have put:
WHERE id = '$_GET[myid]'

In the custom link, i have put "index.php/component/content/article?id=4&myid={rowid}"

Thank you.
 
Assuming your data is on J!'s database, you don't need to hard code all your database details, just use the J! database API. Also, don't EVER use un-sanitized form or query string inputs in a query, as doing so opens you up to SQL injection attacks ...

https://xkcd.com/327/

... so use J!'s input handling and db quote those as well.

Code:
$app = JFactory::getApplication();
$myId = $app->input->getInt('myid', 0);
if (!empty($myId)) {
   $myDb = JFactory::getDbo();
   $myQuery =$myDb->getQuery(true);
   $myQuery->select('foo, bar')->from('mytable')->where('id = ', $myDb->quote($myId));
   $myDb->setQuery($myQuery);
   $myResult = $myDb->loadObject();
   if (!empty($myResult)) {
      echo "Foo is " . $myResult->foo . " and bar is " . $myResult->bar;
   }
}

-- hugh
 
OK. Although it still means having to hardcode your credentials, it's still probably better to use the J! API, as it's so much easier to use than the native stuff, you can sue the query builder, eror handling is easier, etc. And definitely still use the J! input handling.

Code:
$app = JFactory::getApplication();
$myId = $app->input->getInt('myid', 0);
if (!empty($myId)) {
   $myOptions = array();
   $myOptions['driver'] = 'mysqli';
   $myOptions['host'] = 'db.myhost.com'; // append :xxx if you need a custom port
   $myOptions['user'] = 'myuser';
   $myOptions['password'] = 'mypasswd';
   $myOptions['database'] = 'mydatabase';
   $myOptions['prefix'] = 'myprefix_';  // can be empty
   $myDb = JDatabaseDriver::getInstance($myOptions);
   $myQuery =$myDb->getQuery(true);
   $myQuery->select('foo, bar')->from('mytable')->where('id = ', $myDb->quote($myId));
   $myDb->setQuery($myQuery);
   $myResult = $myDb->loadObject();
   if (!empty($myResult)) {
      echo "Foo is " . $myResult->foo . " and bar is " . $myResult->bar;
   }
}

-- hugh
 
It's not essential, the built in PHP stuff works, but it's good to get in the habit of taking advantage of J!'s APIs to do some of the heavy lifting for you.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top