Search code examples
databasejoomla

how can i combine href with id number


Trying to get some data from db but I can't use href with id number in code.

I tried everything but coldn't make it.

 <?php 
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select(array($db->quoteName('title')));
    $query->select(array($db->quoteName('id')));
    $query->select(array($db->quoteName('start_date')));
    $query->select(array($db->quoteName('end_date')));
    $query->from($db->quoteName('#__rbid_auctions'));
    $db->setQuery($query);

    $results = $db->loadObjectList();

    // display the results
    foreach ( $results as $result) {
       echo <<<HTML
    <a href="index.php?option=com_rbids&task=viewbids&id={<?php echo $id; ?>}">$result->title . </a>
    HTML;
       echo "<p>" . $result->start_date . "</p>";
       echo "<p>" . $result->end_date . "</p>";
    }

    ?>

I will be appreciated if someone help me.

Thanks in advance


Solution

  • Demonstration of issue then my suggested solution: (Online Demo)

    $result = new stdClass();
    $result->id = 333;
    $result->title = 'title text';
    
    echo <<<HTML
    <a href="index.php?option=com_rbids&task=viewbids&id={<?php echo $id; ?>}">$result->title . </a>
    HTML;
    

    Output:

    Notice: Undefined variable: id in /in/s1YZG on line 7

    <a href="index.php?option=com_rbids&task=viewbids&id={<?php echo ; ?>}">title text . </a>
    
    • Notice that $id isn't a declared variable. If it was, it would be rendered but all characters between the curly braces are treated literally because you are trying to echo within an echo.

    Without heredoc syntax (heredoc can be funny about tabbing depending on php version):

    echo "<a href=\"index.php?option=com_rbids&task=viewbids&id={$result->id}\">{$result->title} . </a>";  // curly braces may help IDEs with highlighting
    

    New Output:

    <a href="index.php?option=com_rbids&task=viewbids&id=333">title text . </a>
    

    As for your query building syntax...

    • You can save some typing and chain the method calls onto getQuery().
    • None of the quoteName() calls are necessary for stability/security, but if you insist on toeing Joomla's preferred practices, you can call quoteName() on the array in select().

    Suggested Code:

    $db = JFactory::getDbo();
    $query = $db->getQuery(true)
                ->select($db->quoteName(array('title', 'id', 'start_date', 'end_date')))
                ->from($db->quoteName('#__rbid_auctions'));
    $db->setQuery($query);
    
    if (!$results = $db->loadObjectList()) {
        echo "No results";
    } else {
        foreach ($results as $row) {
            echo "<a href=\"index.php?option=com_rbids&task=viewbids&id={$row->id}\">{$row->title} . </a>";       
            echo "<p>{$row->start_date}</p>";
            echo "<p>{$row->end_date}</p>";
        }
    }
    

    Here is another post where loadObjectList() is called after a SELECT query which includes query error checking: https://joomla.stackexchange.com/a/22963/12352

    When you have Joomla questions, please post them on Joomla Stack Exchange.