仅显示Array中匹配ID的结果?

时间:2018-04-23 09:20:33

标签: php joomla

编辑:问题已解决

我必须使用“loadColumn”作为第一个查询,并将“数组”更改为$ results。这就是全部!很高兴它现在正在工作。感谢所有答案:)我将在页面底部发布更新的代码。

-------------------------------------------- -------------------------------------------------- -----------------------------------

我想根据状态字段显示数据库中的某些值,该字段的值为“待定”。假设我在我的网站上提交了一份表格。它的“SubmissionId”为“50”。因此,诸如“Container1”,“Status1”,“Date1”,“BayanNo1”等多个值都可以具有SAME SubmissionId。

这是我编写的代码,但在运行页面时没有加载:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:fitsSystemWindows="true"
android:id="@+id/top_parent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
    layout="@layout/Toolbar"
    android:id="@+id/toolbar_content" />
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:layout_below="@id/toolbar_content"
    android:id="@+id/gridLayout1">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        android:id="@+id/cover_layout"
        android:layout_marginBottom="0.0dp">
        <ImageView
            android:layout_width="500dp"
            android:layout_height="400dp"
            android:id="@+id/imageview_cover"

            android:background="@color/gray"
            android:layout_centerHorizontal="true"
            android:layout_centerInParent="true"
            android:layout_centerVertical="true" />
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/linearLayout1"
            android:layout_alignParentRight="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true">
            <TextView
                android:id="@+id/textview_title"
                android:gravity="left"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:text="Audio Book Title"
                android:textSize="@dimen/text_very_huge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true" />
            <TextView
                android:id="@+id/textview_author"
                android:gravity="left"
                android:textColor="@color/colorAccent"
                android:text="Author"
                android:textSize="@dimen/text_small"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:singleLine="true" />
        </LinearLayout>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/cover_layout"
        android:id="@+id/relativeLayout2"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:paddingBottom="8dp"
        android:paddingTop="8dp"
        android:background="@color/white"
        android:layout_marginTop="0.0dp">
        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/relativeLayout3"
            android:layout_alignParentRight="true"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="@dimen/element_margin_tiny"
            android:layout_marginBottom="@dimen/element_margin_tiny">
            <TextView
                android:text="Position"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textview_position"
                android:gravity="center"
                android:layout_marginLeft="@dimen/element_margin_large"
                android:textColor="@color/black"
                android:layout_centerVertical="true" />
            <SeekBar
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/textview_position"
                android:id="@+id/player_seekbar"
                android:layout_toLeftOf="@+id/textview_duration"
                android:clickable="true"
                style="@style/GreenSeekBar"
                android:layout_centerVertical="true" />
            <TextView
                android:text="Duration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/textview_duration"
                android:layout_alignParentRight="true"
                android:layout_alignBottom="@+id/seekBar1"
                android:layout_alignTop="@+id/seekBar1"
                android:gravity="center"
                android:layout_marginRight="@dimen/element_margin_large"
                android:textColor="@color/black"
                android:layout_centerVertical="true" />
        </RelativeLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:id="@+id/relativeLayout5"
            android:layout_below="@id/relativeLayout3"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">
            <ToggleButton
                android:src="@drawable/toggle_sound"
                android:id="@+id/btnSound"                  
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:textOff=" "
                android:textOn=" "
                android:layout_weight="2"
                android:scaleType="center"
                android:paddingLeft="@dimen/element_margin_tiny" />
            <ImageButton
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:id="@+id/btnPrevious"
                android:background="@null"
                android:src="@drawable/button_previous"
                android:scaleType="center"
                android:layout_toRightOf="@+id/btnSound"
                android:layout_weight="2" />
            <ToggleButton
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:id="@+id/btnPlayPause"
                android:src="@drawable/togglebutton_playpause"
                android:layout_centerVertical="true"
                android:scaleType="center"
                android:textOff=" "
                android:textOn=" "
                android:layout_toRightOf="@+id/btnPrevious"
                android:layout_toLeftOf="@+id/btnNext"
                android:layout_weight="2" />
            <ImageButton
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:id="@+id/btnNext"
                android:background="@null"
                android:src="@drawable/button_next"
                android:scaleType="center"
                android:layout_centerVertical="true"
                android:layout_toLeftOf="@+id/btnRepeat"
                android:layout_weight="2" />
            <ToggleButton
                android:src="@drawable/toggle_playlist"
                android:id="@+id/btnRepeat"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_centerVertical="true"
                android:textOff=" "
                android:textOn=" "                  
                android:scaleType="center"
                android:layout_weight="2" />
        </LinearLayout>
    </RelativeLayout>
</RelativeLayout>

以下是更新且有效的代码:

<?php 

    $db = JFactory::getDbo();
    $db->getQuery(true);

    $db->setQuery("SELECT `SubmissionId` FROM `jos_rsform_submission_values` WHERE `FieldName` = \"Status1\" AND `FieldValue` = \"Pending\"");
    $results = $db->loadObjectList();

    //First query which gets the SubmissionId where "Status" is "Pending"
    //Trying to add these SubmissionId's into an array (don't know if correct or not)
    $arr = [$results];

    //Second query which must display the ContainerNumber based on SubmissionId (Some values are from the same form that the user submitted, that is why the Status and ContainerNumber have the same SubmissionId

    $db->getQuery(true);
    $db->setQuery("SELECT `SubmissionId`, `FieldValue` FROM `jos_rsform_submission_values` WHERE `FieldName` = \"Container1\" AND `SubmissionId` IN '" .$arr. "' "); 

    $result = $db->loadObjectList();

    foreach($result as $value) {
        foreach($value as $key => $data) { 
          echo $data."<br />"; 
       }
   }
?>

2 个答案:

答案 0 :(得分:1)

SQL中IN的语法是:select * from foo where bar in ('1','2', '3')

$db = JFactory::getDbo();
$db->getQuery(true);

$db->setQuery("SELECT `SubmissionId` FROM `jos_rsform_submission_values` WHERE `FieldName` = \"Status1\" AND `FieldValue` = \"Pending\"");
$results = $db->loadObjectList();

//First query which gets the SubmissionId where "Status" is "Pending"
//Trying to add these SubmissionId's into an array (don't know if correct or not)

$arr = array_map($db->quote, $results));
//Second query which must display the ContainerNumber based on SubmissionId (Some values are from the same form that the user submitted, that is why the Status and ContainerNumber have the same SubmissionId

$db->getQuery(true);
$db->setQuery("SELECT `SubmissionId`, `FieldValue` FROM `jos_rsform_submission_values` WHERE `FieldName` = \"Container1\" AND `SubmissionId` IN (" .implode(',', $arr). ") "); 

$result = $db->loadObjectList();

foreach($result as $value) {
    foreach($value as $key => $data) { 
      echo $data."<br />"; 
   }
}

代码使用array_map引用数组,并在IN表达式中使用它。

答案 1 :(得分:0)

尝试time中的in_array()。我希望它能解决您的问题。