Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:19:10+00:00 2026-06-06T09:19:10+00:00

I am using this function to list items from table helps depending on parameters:

  • 0

I am using this function to list items from table helps depending on parameters: (I included most of code but you can really just focus on the mysql querys)

function list_helps($by,$value,$page = -1,$ipp = 20){
    /* Yes I concatenate querys... so i use some variables to help it a bit */
    $sql = 'SELECT helps.* FROM helps ';
    $where = ''; $orderBy = ''; $in = ''; $join = ''; $limit = '';  $resultitems = ''; $header = '';
    if($page > 0)   $limit = 'LIMIT '.$page*$ipp.', '.($page+1)*$ipp; else $limit = 'LIMIT 10';
    switch($by){

        case 'byuser':
            $where = 'WHERE id_user ='.$value;
            $orderBy = 'ORDER BY id DESC';
            break;
        case 'byfriend':
            $sql = 'SELECT
              h.*,
              f.* 
            FROM (
              SELECT
                id,
                CASE followerid WHEN '.$value.' THEN followingid ELSE followerid END AS friend_id
              FROM friends
              WHERE acepted = 1
                AND (followerid  = '.$value.' OR followingid = '.$value.')
            ) AS f
              INNER JOIN helps AS h ON h.id_user = f.friend_id
              ORDER BY h.id DESC';
            break;
        default:
            break;
    }
    $sql .= $where.' '.$orderBy.' '.$limit;
    $res = cache_query($sql,'',60*60*5);
    /* checks in cache first, if not; executes query.. next code it's to render content */
}

There are more cases but lets focus on those two.

They both serve the ‘helps’ that are being requested (if user, or if users’s friend); The problem is when i am trying to retrieve the comments of those (like if it where a blog post); which I am doing like so:

res = cache_query('SELECT help_replies.content, help_replies.date, 
            help_replies.offers, help_replies.accepted, help_replies.id_responds,
            usuarios.first_name, usuarios.last_name, usuarios.avatar,usuarios.id
        FROM help_replies left join usuarios 
            ON  help_replies.id_user = usuarios.id
        WHERE  help_replies.id_responds = '.$this->id.' 
        ORDER BY help_replies.id ASC', '', 30);

foreach($res as $obj) {
    /* $obj['id_responds'] */
    }
}

The thing is that the help_replies.id_responds makes reference to the item, so when the case is ‘byuser’ is fine; but when the case is ‘byfriend’ this field is lost (so is showing always the same comments)

Conclusion:

  • byuser -> helps fine & comments fine
  • byfriend -> helps fine (which is wierd) & comments wrong

Any Idea why $this->id is not as expected from the (JOINED) query?

-EDIT-

$this->id comes from this construct method (i ommited many atributes)

function __construct($item) {
    $this->id = $item['id'];
    $this->id_user = $item['id_user'];
    ..  
}

And $item is one iteration of the foreach (so its the returned row converted to array)

Muchas Gracias

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:19:11+00:00Added an answer on June 6, 2026 at 9:19 am

    Your byfriends query executed by function list_helps(...) appears to return a field labelled id twice; once for your helps table and then again for f the alias of the friends table, as part of the sub-query.

    I can imagine that if $this->id contains a friends id and not the helps id then your comments query (help_replies) will return the wrong results.

    So perhaps changing your sub-query with an alias for the friends id as shown ie id AS friendsId, everything will work as expected?

    function list_helps($by,$value,$page = -1,$ipp = 20){
        ...
        ...
        case 'byfriend':
            $sql = 'SELECT
                h.*,
                f.* 
            FROM (
                SELECT
                    id  AS friendsId,
                    CASE followerid WHEN '.$value.' THEN followingid ELSE followerid END AS friend_id
                FROM friends
                WHERE acepted = 1
                AND (followerid  = '.$value.' OR followingid = '.$value.')
            ) AS f
                INNER JOIN helps AS h ON h.id_user = f.friend_id
                ORDER BY h.id DESC';
            break;
    

    I’ve got into the habit of naming my table identifier field <tablename>Id for this very reason and saves having to use alias everywhere.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m un-animating a list using this code... function unanimate_li() { $li.filter(':last') .animate({ height:
My little site should be pooling list of items from a table using the
I'm using this function to extract files from .zip archive and store it on
I'm using this function: preg_match_all('/((\d{9})\.html)/s', $content, $results); It works fine but i just want
can someone please help me with this code, i'm missing something but can't figure
I am using this function to close existing form and open a new form.
I am using this function https://www.php.net/sys_getloadavg to get a hint about my server load.
Dear all i am using this function to get the GMT time, i am
so I'm deploying a shipping calculator using this function , and I'm getting this
i am making an upload with Ajaxupload plugin and i am using this function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.