简体   繁体   English

Using MySQL and PHP query (with parentheses) to result in output of the 24 rows of html data I want based on value matching in two tables

[英]Using MySQL and PHP query (with parentheses) to result in output of the 24 rows of html data I want based on value matching in two tables

My PHP, MySQL, HTML query and output here results in output of the first 24 rows, without errors, without using WHERE, JOIN, = or HAVING... But the second batch of 24 rows, which I want as a result, are to be based on (table b) b.id (one row) having (table a) a.record (all 24 rows) while (table b) b.username = $user-username (variable from getuser() ). My PHP, MySQL, HTML query and output here results in output of the first 24 rows, without errors, without using WHERE, JOIN, = or HAVING... But the second batch of 24 rows, which I want as a result, are基于 (table b) b.id (一行) 具有 (table a) a.record (所有 24 行) 而 (table b) b.username = $user-username (变量来自 getuser() )。 I am lost at the place where defining WHERE statements (see below).我迷失在定义 WHERE 语句的地方(见下文)。

The two rows of values at b.id reads a 7 (which happens to be the sequential generated value) for $user2 and reads a 6 (one numeral lower in sequence) for $user1. b.id 的两行值为 $user2 读取 7(恰好是顺序生成的值),为 $user1 读取 6(顺序低一个数字)。 Each user will be submitting information using a BreezingForms app in Joomla.每个用户都将使用 Joomla 中的 BreezingForms 应用程序提交信息。 Breezing forms splits the form submission into two separate tables. Breeze forms 将表单提交拆分为两个单独的表。 Their own Crosstec Joomla backside function does almost what I want using selection drop downs.他们自己的 Crosstec Joomla 背面 function 几乎可以使用选择下拉菜单完成我想要的操作。 (Maybe I can figure out how to borrow that code.) (也许我可以弄清楚如何借用该代码。)

I am trying to get the "24 rows of data" that I want which were input into the database as values unique to the logged in user as shown in b.id and b.username, which is the same as $user->username as above.我正在尝试获取我想要的“24 行数据”,这些数据作为登录用户的唯一值输入数据库,如 b.id 和 b.username 所示,与 $user->username 相同如上。 My very next submission will increment b.id and a.record to number 8. b.id is one row of data and a.record is 24 rows of disparate data from the webform.我的下一次提交会将 b.id 和 a.record 增加到 8 号。b.id 是一行数据,a.record 是来自网络表单的 24 行不同数据。

When everything is done with this query, I want each user to have their own Joomla Article page that will give them back their most recent 24 rows of data (facileforms puts the most recent at the bottom of table a and table b.) for any given user as matched up between b.username and $user->username as the clue as to which 24 rows in a.record, a.title, a.value will be sent to the html portion.完成此查询的所有操作后,我希望每个用户都有自己的 Joomla 文章页面,该页面将返回他们最近的 24 行数据(facileforms 将最新的数据放在表 a 和表 b 的底部。)对于任何给定用户在 b.username 和 $user->username 之间匹配作为线索,将 a.record、a.title、a.value 中的 24 行发送到 html 部分。

I am testing with two different usernames and I can get the 24 rows of table a where a.record is (7) if I input that '7' in place of 'b.id' but that hard-coding defeats the purpose.我正在使用两个不同的用户名进行测试,如果我输入“7”代替“b.id”,我可以获得表 a 的 24 行,其中 a.record 为 (7),但硬编码会破坏目的。 I want to get dynamic values to look into the $db values which are expected to increment all the time and compare them with the $user->username to generate the correct select.我想获取动态值来查看预计会一直增加的 $db 值,并将它们与 $user->username 进行比较以生成正确的 select。

<p>
<?php 
$user = JFactory::getUser(); echo "<p>Username: {$user->username} • User ID: {$user->id} </p>";  
$db = JFactory::getDbo(); 
$query = $db->getQuery(true); 
$query 
   ->from($db->quoteName('#__facileforms_subrecords', 'a'), ($db->quoteName('#__facileforms_records', 'b')))  
   ->select($db->quoteName(array('a.record', 'a.title', 'a.value')), ($db->quoteName(array('b.id', 'b.username'))))  
   ->where( I am lost in defining conditions, see below )
   ->order('a.id ASC') 
   ->setLimit('24');  
$db->setQuery($query); 
$results = $db->loadObjectList(); 
?>
<table class="table table-striped well">
<div align="left">
   <tr>
            <th><h2>Record</h2></th>
            <th><h2>Title</h2></th>
            <th><h2>Value on File</h2></th>
  </tr> 
<?php foreach($results as $result) { ?>
    <tr>
          <th><?php echo $result->record; ?></th>
          <th><?php echo $result->title; ?></th>
          <td><?php echo $result->value; ?></td>
    </tr>  
<?php  }  ?>
</div>
</table>
</p>

When I use this as my WHERE clause it gives me those 24 rows of records with a 7 and if I change to 6 I get those 24 rows with a 6. I want the value to increment WHEN either b.id or a.record change based on which username input a record using my form.当我使用它作为我的 WHERE 子句时,它为我提供了 7 的那 24 行记录,如果我更改为 6,我将获得 6 的那 24 行。我希望值在 b.id 或 a.record 更改时增加基于哪个用户名使用我的表单输入记录。 My form captures their username in their batch of 24 rows of values sent to table a and single row of values sent to table b.我的表单在发送到表 a 的 24 行值和发送到表 b 的单行值中捕获他们的用户名。

Here is a somewhat working WHERE clause:这是一个有点工作的 WHERE 子句:

   ->where($db->quoteName('a.record') . ' LIKE ' . $db->quote(7)) 

This next WHERE clause gives me back the first row (1 of 24) of data but this time it is only resulting in the first row of a.value based on the logged in user as shown in b.username - just not giving me all 24 rows:下一个 WHERE 子句将第一行(24 个中的 1 个)数据返回给我,但这次它仅根据登录用户生成 a.value 的第一行,如 b.username 所示 - 只是没有给我全部24 行:

   ->where($db->quoteName('a.value') . ' = ' . $db->quote($user->username))  

Can someone please take a look and work with me?有人可以看看并和我一起工作吗? Thanks from a newbie!来自新手的感谢!

" ¯\_(ツ)_/¯ "


Table a
+-----------+------------+---------+
|  Record   |   Title    |  Value  |
+-----------+------------+---------+
|     8     |  Username  |  Value  | <- Row 1
+-----------+------------+---------+
|     8     |  Point 1   |  Value  | <- Row 2
+-----------+------------+---------+
|     8     |  Point 2   |  Value  | <- Row 3
+-----------+------------+---------+
Up to 24 rows as per setLimit. 24 new rows arrive at each submit form. 


Table b
+-----------+------------+
|    id     |  username  |
+-----------+------------+
|     6     |  Username1 |  <- User 1 Row 1
+-----------+------------+
|     7     |  Username2 |  <- User 2 Row 25
+-----------+------------+
|     8     |  Username3 |  <- User 3 Row 50 and growing by 24 rows
+-----------+------------+

Row id number increments as form is submitted and username is captured with the form.提交表单时,行 ID 编号会增加,并且用户名与表单一起被捕获。

Use a join instead of using a from with two tables.使用联接而不是对两个表使用 from。

SQL Statement SQL 声明

SELECT a.`record`, a.`title`, a.`value`
FROM `#__facileforms_records` b
JOIN `#__facileforms_subrecords` a
    ON a.`Record` = b.`id`
WHERE b.`id` = 8
ORDER BY a.`id`
LIMIT 24;

Joomla PHP Code Joomla PHP 代码

$query
    ->select($db->quoteName(array('a.record', 'a.title', 'a.value')))
    ->from($db->quoteName('#__facileforms_records', 'b'))
    ->join('LEFT',$db->quoteName('#__facileforms_subrecords', 'a'). 'ON ' . $db->quoteName('a.Record') . ' = ' . $db->quoteName('b.id'))
    ->where($db->quoteName(('b.id') . ' = ' . '8')
    ->order($db->quoteName('a.id') . ' ASC')
    ->limit('24');

Note: Code not tested - it may contain some typos.注意:代码未经测试- 它可能包含一些拼写错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM