0

On my old website, built in php i had a link on a page, wich picked an ID based on what line it was.

<td width=124><a href="tp/package.php?ID=<?php echo $row[0];?>">Package</a></td>

When the link was pressed, it brought that ID with it, and helped populate the next page (package.php) with values from mysql based on that ID.

Like:

$query = "SELECT * FROM `TABLE_NAME` WHERE `ID`='" . $id . "'";

Now i am trying to do the same in Joomla. I have the link ready

echo '<td class="first"><a href="index.php/package?ID='. $row->ID .'">

But i cannot figure out how to write the code on that page, so it can populate the table based on that.

My current code i am experimenting with on the package-page:

$option = array();
$option['driver'] = 'mysql';
$option['host'] = 'HOST';
$option['user'] = 'USER';
$option['password'] = 'PASSWORD';
$option['database'] = 'DATABASE';
$option['prefix'] = '';

$db = JDatabaseDriver::getInstance($option);

$query = $db->getQuery(true);
$query->select($db->quoteName(array('ID', 'DESCRIPTION', 'MGRS')));
$query->from($db->quoteName('redsands_strike'));
$query = "SELECT * FROM `redsands_strike` WHERE `ID`='" . $id . "'";

$db->setQuery($query);$results = $db->loadObjectList();

while($row = mysqli_fetch_array($result)){ 
echo "<table id='customers' >";
                echo "<tr>";
                echo "<th>T/ID#:</th>";
                echo "<th>" . $row['ID'] . "</th>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>Description:</th>";
                echo "<td>" . $row['DESCRIPTION'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>MGRS:</th>";
                echo "<td>" . $row['MGRS'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>LL DDM:</th>";
                echo "<td>" . $row['DDM'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>LL DMS:</th>";
                echo "<td>" . $row['DMS'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>Altitude:</th>";
                echo "<td>" . $row['ALT'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>Recon images:</th>";
                echo '<td><img src="/tp/'. $row['ID'] .'_a.jpg"/><br><br><img src="/tp/'. $row['ID'] .'_b.jpg"/></td>';
                echo "</tr>";


}
echo "</table>";

But this doesnt seem to be working.

My first question here, so i hope i got it clear and easy to understand.

My goal is to have one "article template" page called package, wich will pull the proper information based on the ID given from URL that is brought by pressing the link from the page before

  • ...just so you know $query = "SELECT * FROM `redsands_strike` WHERE `ID`='" . $id . "'"; will be overwriting the three previous lines (rendering them useless). What are you getting from JFactory::getApplication()->enqueueMessage('id = ' . $id, 'notice'); ? Telling us that something doesnt seem to be working is not terribly articulate information. Please better describe how your code is not working. You see no page output? Missing output, errors in your error log? White screen of death? You need to be iterating $results not calling mysqli_fetch_array() – mickmackusa Nov 30 at 11:03
  • The code i posted was my last example of that i tried. The page before is working as intended, with the link carrying on the ID to the package page. And on the package page i only see the table layout, but its not reading the database based on what the ID in the url is. – Frosties82 Nov 30 at 11:28
  • I think this answer will show you how to build your query and generate an html table from the result set. joomla.stackexchange.com/a/22977/12352 That said you shouldn't generally qerying from your template. Within the MVC design pattern, the Controller script passes the Model's query result to the View for generating the output. – mickmackusa Nov 30 at 11:44
  • What im struggling with is how i can make the script look for the ID based on the url. Old code was this: $query = "SELECT * FROM redsands_strike WHERE ID='" . $id . "'"; That last part: WHERE ID='" . $id . " selected the information to load based on ID that was in the URL: package.php?ID=114917376 – Frosties82 Nov 30 at 15:20
0

I got it working, by using the entire code from before. Didnt even think about testing it int its purity since i assumed that the connection needed to be rebuilt. But it worked with a simple copy/paste.

Thanks for all help!

  • So what exactly did you write as a finished script? You didn't abandon Joomla methods entirely did you? – mickmackusa Nov 30 at 21:41
0
Just the same code as i had on my old clean php website. Copied the code and pasted it and it worked right away. 
Might work on it more when i have the website opened. Right now im all for it to work. 

// Create connection
$conn = new mysqli($servername, $username, $password, $db_name);
// Check connection
if ($conn->connect_error){
  die("Connection failed: " . $conn->connect_error);
} 
$id = $_GET['ID'];
$id = mysqli_real_escape_string($conn,$id);
$query = "SELECT ID,SUBSTRING(DESCRIPTION, 4) AS DESCRIPTION, MGRS, DDM, DMS, ALT FROM `redsands_strike` WHERE `ID`='" . $id . "'";
$result = mysqli_query($conn,$query);

while($row = mysqli_fetch_array($result)){
echo "<table id='customers' >";
                echo "<tr>";
                echo "<th>T/ID#:</th>";
                echo "<td>" . $row['ID'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>Description:</th>";
                echo "<td>" . $row['DESCRIPTION'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>MGRS:</th>";
                echo "<td>" . $row['MGRS'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>LL DDM:</th>";
                echo "<td>" . $row['DDM'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>LL DMS:</th>";
                echo "<td>" . $row['DMS'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th>Altitude:</th>";
                echo "<td>" . $row['ALT'] . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<th valign='top'>Recon images:</th>";
                echo '<td><img src="/tp/'. $row['ID'] .'_a.jpg"/><br><br><img src="/tp/'. $row['ID'] .'_b.jpg"/></td>';
                echo "</tr>";

            }
echo "</table>";

?>
  • This snippet is not using any of Joomla's built-in features and is completely "detached" from Joomla's security checks. The technique in this answer should not be avoided by Joomla researchers. @Frosties I meant for you to edit your first answer to include your work, not post a second answer. Please transfer this answer to your first answer as an edit, then delete this answer. Please take the tour. – mickmackusa Dec 1 at 18:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.