This question already has answers here:
Extracting Twitter hashtag from string in PHP
(7 answers)
Closed 8 years ago.
How to get specific value of query in MySQL.
My code :
$q = mysqli_query("SELECT * FROM tb_post WHERE post LIKE '%#%'");
while($d = mysqli_fetch_array($q))
{
$post = $d['post'];
}
Example data result like this :
I'm groot #me
What are you doing #what
Sometimes I felt like superman #me
I don't know what to do #confuse
Now, my question is : is it possible to get just hashtag? Example : #me, #what, #confuse.
So I do not need another value.
Please help!
This should work for you:
$post = $d['post'];
$whatIWant = substr($post, strpos($post, "#") + 1);
echo "#" . $whatIWant . "<br />";
Output:
#me
#what
#me
#confuse
Related
This question already has answers here:
How can I set the default value for an HTML <select> element?
(34 answers)
Closed 6 years ago.
I have an issue where by I want to save a user input and then allow them to edit it later.
when i list the results for the edit their is no default.
<select id="edit_group" name="edit_group" class="form-control">
<?php Types("") ?>
</select>
types function is
function Types($x){
global $mysqli;
$type = " SELECT type FROM types ";
if ($type_results = $mysqli->query($type)) {
while ($row = $type_results->fetch_assoc()) {
printf ("<option>%s</option>", $row["type"]);
}
$type_results->free();
}
}
if anyone could point me in the right direction of how i could set the default value so its the one stored in the db, it would really help a beginner out. or better still point me in the right direction of documentation as my google searches have returned nothing but json and java options.
You don't use an argument $x of your Types function which as i guess is a selected value. Try to replace your function with this code:
function Types($x) {
global $mysqli;
$type = " SELECT type FROM types ";
if ($type_results = $mysqli->query($type)) {
while ($row = $type_results->fetch_assoc()) {
$selected = ($row["type"] == $x) ? 'selected="selected"' : '';
printf ("<option %s>%s</option>", $selected, $row["type"]);
}
$type_results->free();
}
}
and of course provide selected value to function Types in your html code.
This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
Hi I have created a while loop but cannot understand why it loops forever. Could somebody please explain why?
while ($i<3){
if ($i=1){
$x='psu';
}else if ($i=2){
$x='cases';
}
$sqlcpu = "SELECT * FROM $x WHERE name LIKE '%{$term}%'";
$query = mysqli_query($db, $sqlcpu);
while ($row = mysqli_fetch_array($query)){
?><br /> Name: <?php echo $row['name'];?>
<?php
echo ' Price: £'.$row['price'];
}
$i++;
}
There are other problems with this code but they are not my main concern right now as this loop seems like it should be simple. The variables $x and $i never change after $i=1
if ($i=1) You're assinging 1 to $i here, instead of comparing 1 and $i, use
if ($i==1)
instead.
Instead of the for/if-elseif construct you could also use
foreach( array('psu', 'cases') as $table ) {
$sqlcpu = "SELECT * FROM $table WHERE name LIKE '%{$term}%'";
This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 8 years ago.
$i=0;
$sql = $dbh->prepare('SELECT kappaleen_nimi, levy_id, kesto FROM kappaleet');
$ok = $sql->execute();
if(!$ok) { print_r( $sql->errorInfo() ) ; }
while($row = $sql->fetch(PDO::FETCH_ASSOC) ) {
foreach($row as $value) {
if($value!=null || $value!=0){
$haku2[$i] = $value;
$i=$i+1;
}
}
}
I'm working with this sql database, where i'm fetching kappaleen_nimi (name of the track), levy_id (record_id) and kesto (length) from the sql table kappaleet. I'm trying to remove fields containing null or 0. However, the final zero gets through anyway through this filter: "if($value!=null || $value!=0)".
When printing my table the following way:
foreach($haku2 as $value){
echo $value.', ';
}
I get this kind of result (last few fields): "Sateen tango, 7, 3.11, 0, "
The final zero is still there, and i can't get my head around it, why the he** it passes the if condition... I know that the final zero is levy_id (record_id).
I think you are looking for an AND conditional instead of an OR. To fix this, replace the following line
if($value!=null || $value!=0){
with this one
if($value!=null && $value!=0){
I have a php script for conducting online test. while taking test, I am using post method to get the answers in the action page.The answers are passed as an array with question id and the selected option value(true or false/the answers the candidate entered like that).Then I have to insert the answers marked by the candidate in the database. The code is as shown below:
$student_id=$_POST['name'];
$survey_id=$_POST['survey_id'];
$store = array();
if (isset($_POST['question_id'])) {
foreach ($_POST['question_id'] as $key => $option) {
$option1 = array_filter($option);
print_r($option1);
if (count($option1) > 1) {
$option2 = implode("^", $option1);
$store[] = $option2;
} else {
$value = $option1;
$i = implode(null, $option1);
$store[] = $i;
}
}
print_r($store);
}
$t = new DateTime();
$t->setTimestamp($time = time());
$t->setTimeZone(new DateTimeZone("Asia/Singapore"));
$date = $t->format(DateTime::RFC850);
$SQL = "INSERT INTO answer_table(student_id ,survey_id, ans_1, ans_2, ans_3, ans_4, ans_5, ans_6, ans_7, ans_8, ans_9, ans_10, timestamp) VALUES ('$student_id','$survey_id', '$store[0]', '$store[1]', '$store[2]', '$store[3]', '$store[4]', '$store[5]', '$store[6]', '$store[7]', '$store[8]', '$store[9]', '$date')";
$result = mysql_query($SQL);
If the student answers the test in sequential order(from question number 1 to 10) the code works fine. But when the candidate answers in random manner(first 10 then 5 like that),the table field named,ans_1 will get inserted with answer of question number 10. I need to insert fields with corresponding answers ,(ans_1 with answer of question 1 like that) what ever pattern,the candidate takes test.
Can anyone help me to solve this issue. Thanks in advance.
For achieving you goal.you should store question_id with answers or create another table which store question_id and corresponding answers, with id of answer table.
If you have the question id in the $key variable, you could use
$store[$key] = $option2;
instead of
$store[] = $option2;
With that the first question is the first entry in the array regardless of the order of the answers in the post array.
You can replace $store[] = $option2; with:
$store[$_POST['question_id']] = $option2;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP & MySQL Pagination
I'm a bit confused as to how to add pagination to my mysql query, since I am looping through the results and adding them to a variable each time:
$DBQuery3 = mysqli_query($dblink, "SELECT * FROM images WHERE project_id = '$FormProjectID'");
$ProjectContent ='';
while($row = mysqli_fetch_array($DBQuery3)) {
$DBImageID = $row['image_id'];
$DBProjectID = $row['project_id'];
$DBImageName = $row['image_name'];
$DBImageDescription = $row['image_description'];
$DBDateCreated = $row['date_created'];
$DBLinkToFile = $row['link_to_file'];
$DBLinkToThumb = $row['link_to_thumbnail'];
$DBGivenName = $row['given_name'];
//if the image was given a name by the user, display it
//otherwise display the generated name
if (strlen($DBGivenName) > 1) {
$FileName = $DBGivenName;
} else {
$FileName = $DBImageName;
}
$ProjectContent .= '
<img src="uploads/'.$DBLinkToThumb.'" width="150px" height="150px" alt="'.$FileName.'" title="'.$FileName.'"/>
';
Any pointing in the right direction would be greatly appreciated.
Hy,
Try use a php pagination script, there are a lot on the net, just search for those words.
For example this: http://www.coursesweb.net/php-mysql/pagination-class-script_s2 can paginate data stored into a database, or into an array.