简体   繁体   中英

How can I query a database based on a user text input?

So I am creating a Lorem Ipsum generator and I am stuck.

I have created a table called "wutangsoldiers", which contains the id of the user, their name, and each row has a different lyric. So far I have this:

$id = get_the_ID();
global $wpdb;
$lyrics = $wpdb->get_results("SELECT * FROM `wutangsoliders` WHERE `name` = '$id' ");

Inside of the generated text div, I have

<?php foreach ($lyrics as $lyrics) {
?>
<p><?php echo $lyrics->text;?></p>
<?php } ?>

How would I limit the number of "lyrics" shown based what the user inputs in <input type="submit" id="blaow" name="blaow">

To get the ID of the user based on a (I assume a text Input) I also assume that you are using HTTP POST METHOD ($_POST)

This is the User Value

<form>

<input type="text" value="1" name="user_value">

<input type="submit" id="blaow" name="blaow">
</form>

This is how to get the value

if(isset($_POST['blaow']){ // if the input has been pressed

   $idUser = $_POST['user_value']; // the value sent from the HTML above

   $lyrics = $wpdb->get_results("SELECT * FROM `wutangsoliders` WHERE `name` = '$idUser' ");

foreach ($lyrics as $lyrics) {

  print "<p>".$lyrics->text."</p>";

}

}

Just get the user input limit that

$userinput = $_GET['lyrics_limit'];

Then put it in sql query

$lyrics = $wpdb->get_results("SELECT * FROM wutangsoliders WHERE name = '$id' limit '$userinput' " );

Then you will get only according to the input by user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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