在HTML页面内使用PHP创建指向另一个页面或目录的操作链接

时间:2019-02-04 01:37:00

标签: php html sql codeigniter-3

我有问题。问题是在HTML页面内使用PHP创建到另一个页面或目录的链接。

这是我的代码

    $query = $this->db->query($sql);                      
     if ($query->num_rows() > 0){
       echo '<a href="<?php echo base_url('admin/edit/'."$g->id_info")?>">Edit</a>';

    } else {                                                       
      echo '<a href="<?php echo base_url('admin/delete/'."$g->id_info")?>" >Delete</a>';
    } 

但是代码返回错误

Parse error: syntax error, unexpected 'admin' (T_STRING), expecting ',' or ';' in D:\xampp\**** on line 150

在HTML页面内用PHP嵌入href的正确方法是什么???预先感谢

1 个答案:

答案 0 :(得分:0)

也许这种方式适合您的情况:

public class BuyerHome extends AppCompatActivity {

    EditText nameIn;
    EditText commentIn;
    TextView viewComment;
    Button postComment;
    ListView olderComments; //<<<<<<<<<< Added (Listview needs to be added to the layout)

    DatabaseHelper2 myDB;
    Cursor oldermessages, latestmessage; //<<<<<<<<<< Added
    SimpleCursorAdapter sma; //<<<<<<<<<< Added adapter for the ListView

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buyer_home);

        nameIn = (EditText) findViewById(R.id.nameInput);
        commentIn = (EditText) findViewById(R.id.commentText);
        viewComment = (TextView) findViewById(R.id.viewCommentText);
        olderComments = (ListView) findViewById(R.id.olderComments); //<<<<<<<<<< Added

        myDB = new DatabaseHelper2(this);
        postComment = (Button) findViewById(R.id.buttonComment);
        postComment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = nameIn.getText().toString();
                String comment = commentIn.getText().toString();

                if (nameIn.length() != 0 && commentIn.length() != 0) {
                    myDB.addData(name, comment); //<<<<<<<<<< method starts changed lower case a
                    nameIn.setText("");
                    commentIn.setText("");
                } else {
                    Toast.makeText(BuyerHome.this, "Insert fields",
                            Toast.LENGTH_SHORT).show();
                }
                displayComments(); //<<<<<<<<<< added to refresh the display according to the latest comments
            }
        });
    }

    //<<<<<<<<<< ADDED so will display comments whenever called including returning from another activity
    @Override
    protected void onResume() {
        super.onResume();
        displayComments();
    }

    //<<<<<<<<<< ADDED entire method for displaying comments
    private void displayComments() {
        latestmessage = myDB.getLatestComment();
        if (latestmessage.moveToFirst()) {
            viewComment.setText(latestmessage.getString(latestmessage.getColumnIndex(DatabaseHelper2.COL_COMMENT_NAME)) +
                    " said :- " +
                    latestmessage.getString(latestmessage.getColumnIndex(DatabaseHelper2.COL_COMMENT_COMMENT)));
        } else {
            viewComment.setText("");
        }
        oldermessages = myDB.getAllButLatestComment();
        if (sma == null) {
            sma = new SimpleCursorAdapter(
                    this,
                    android.R.layout.simple_list_item_2,oldermessages,
                    new String[]{DatabaseHelper2.COL_COMMENT_NAME,DatabaseHelper2.COL_COMMENT_COMMENT},
                    new int[]{android.R.id.text1,android.R.id.text2},
                    0
            );
            olderComments.setAdapter(sma);
        } else {
            sma.swapCursor(oldermessages);
        }
    }
}