I have a table in the following structure...
ID USER_ID INTEREST
1 290 soccer
2 290 tennis
3 290 badminton
4 560 netball
I want to grab all values from the table where the user id is equal to the session user id and assign them as variables.
I have the following which works but displays the data very peculiarly..
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$interests[] = $row['interest'];
$interest1 = $interests[1];
$interest2 = $interests[2];
$interest3 = $interests[3];
print $interest1 . " - " . $interest2 . " - " . $interest3;
}
The above however outputs something along the lines of...
- - tennis - - tennis - badminton -
Can anybody see where I'm going wrong?
This should do what you need:
$interestsquery = "SELECT `interest` FROM `user_interests` WHERE `user_id` = " . $usersClass->userID();
$result = mysql_query($interestsquery);
$interests = array();
while(list($interest) = mysql_fetch_array($result))
$interests[] = $interest;
print explode($interests, ' - ');
Think what happens to your loop at the first iteration:
$interest is an empty array
you fetch the first value and put it into $interest[0],
you fill $interest1 with the value that lies into $interest[1] (it is empty)
same for $interest2 and $interest3
you print ""." - ".""." - ".""
in the second run:
$interest is [0=>soccer]
you fetch the second value and put it into $interest[1],
you fill $interest1 with the value that lies into $interest[1] tennis
same for $interest2 and $interest3 (that are still empty)
you print "tennis"." - ".""." - ".""
and so on.
You need print the result when you exit the while loop (and the code is still flawed as
it don't get the value into the index 0 of the array).
An alternative should be:
$interestsquery = "SELECT `interest` FROM `user_interests` WHERE `user_id` = "
. $usersClass->userID();
$result = mysql_query($interestsquery);
$interests = array();
// you fetch just a field, fetch_row will be sufficent
while($interest = mysql_fetch_row($result)) {
array_push($interests, $interest[0]);
}
echo implode(' - ', $interests);
$interests = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$interests[] = $row['interest'];
}
$int_count = count($interests);
$i=0;
foreach ($interests as $interest) {
$var = 'interest' . ($i + 1);
$$var = $interest;
$i++;
}
print $interest1 . " - " . $interest2 . " - " . $interest3;
Related
I want to fetch each element of array in foreach loop.
Here in my code my array is displayed, I want to fetch this array element in if condition.
if (!empty($data[5])) {
$selectsql = "select value_id from catalog_product_entity_media_gallery where entity_id = '".$entity_id."'";
$selectsqlresult = $connection->query($selectsql);
$resultquery = $selectsqlresult->fetchAll();
print_r($resultquery);
if(!empty($resultquery['value_id']))
{
$imgpos = 2;
foreach($resultquery as $value)
{
$updatequery = "update catalog_product_entity_media_gallery_value set position = '".$imgpos."' where value_id = '".$value."' limit 1";
//$connection->query($updatequery);
echo $updatequery . "\n";
echo "Images Position - " . $imgpos ."\n\n"; $imgpos++;
}
}
}
I want to increase images position by 1 as per the array element.
Image position $imgpos is used in update query.
Please provide some guidance.
You could also use the key value in the foreach statement to get a incremented value:
if(!empty($resultquery['value_id']))
{
foreach($resultquery as $imgpos => $value)
{
$updatequery = "update catalog_product_entity_media_gallery_value set position = '".$imgpos."' where value_id = '".$value."' limit 1";
//$connection->query($updatequery);
echo $updatequery . "\n";
echo "Images Position - " . $imgpos ."\n\n"; $imgpos++;
}
}
I'm new to php and I want to separate the records with a comma.
Database:
the table in sql
I use this code to get the data:
<?php
$id = get_the_ID();
$sql = "SELECT *
FROM tablename
WHERE parent_id=$id";
$result = $conn->query($sql);
while($row = mysqli_fetch_array($result))
{
echo "Test: " . $row["value"]. "<br>";
}
mysqli_close($con);
?>
It return twice as:
Test: Test
Test: Test1
I want to separate the records with a ',' like this:
Test: Test, Test1
Store your values in an array and than implode with ",". You will get the result:
$id = get_the_ID();
$sql = "SELECT *
FROM tablename
WHERE parent_id=$id";
$result = $conn->query($sql);
$yourArr = array();
while($row = mysqli_fetch_array($result))
{
$yourArr[] = $row["value"];
//echo "Test: " . $row["value"]. "<br>";
}
echo "Test: ". implode(",",$yourArr);
You can do this entirely in MySQL using GROUP_CONCAT:
<?php
$id = get_the_ID();
$sql = "SELECT `parent_id`, GROUP_CONCAT(`value` SEPARATOR ', ') AS comma_separated_values
FROM tablename
WHERE `parent_id` = '$id'
GROUP BY `parent_id`";
$result = $conn->query($sql);
while ($row = mysqli_fetch_array($result))
{
echo 'Test: ' . $row["comma_separated_values"]; // Test: test, test2
}
mysqli_close($con);
?>
You should change the "comma_separated_values" name to something more appropriate for your data.
In your particular case, you would not need the while() loop either as we're limiting the MySQL to a single row.
If you were to remove the WHEREparent_id= '$id' from the SQL query, then you could return the results of multiple parent_id values. For example:
while ($row = mysqli_fetch_array($result))
{
echo 'Parent ' . $row['parent_id'] .': ' . $row["comma_separated_values"] . '<br>';
}
Would return:
Parent 292: Test1, Test2
Parent 293: Test3, Test4
Parent 294: Test5, Test10, Test50
etc...
you can use update query for this
$query="Update tablename set value=CONCAT(value,',test1') where parentid='295'";
$result=mysqli_query($conn,$query);
So What I want to do is to echo 1 for the first row that is returned and two for the 2nd one and so one, Please I just don't know how to phrase this, I don't want to just count rows from mysql table, I want to give each row a number automatically and echo that number.
Please forgive my deprecated code.
$log = mysql_query("SELECT * FROM $table WHERE $columnid = '$id'") or die (mysql_error());
while($row = mysql_fetch_array($log)){
echo row['name'];
}
Lets assume that the above returns some names. so I want this.
1 : John
2 : Nancy
3 : Dave
I don't want to write those number.
Please take into account ORDER by rate DESC since I'm using that to descent the number by the biggest number of rate.
You could do:
$count = 1;
while($row = mysql_fetch_array($log)){
echo $count . ' ' . row['name'];
$count++;
}
Or:
echo '<ol>';
while($row = mysql_fetch_array($log)){
echo '<li>' . row['name'] . '</li>';
}
echo '</ol>';
$log = mysql_query("SELECT * FROM $table WHERE $columnid = '$id'") or die (mysql_error());
$c = 0
while($row = mysql_fetch_array($log)){
echo row['name'];
$c = $c + 1;
}
echo "Raw Count = ".$c
I'm trying to display ul>li list using PHP while loop and MYSQL.
My database 'company' has table 'news' with 2 columns ' id & content'.
The while loop works correctly it is returning two 'li' = as there are two rows in the table.
But, instate of data from the table something like this $indx." - ".$ID." ".$CONTENT." is inserted in each 'li'.
My while loop:
$readNews_SQLselect = "SELECT ";
$readNews_SQLselect .= "id, content "; // rows names
$readNews_SQLselect .= "FROM ";
$readNews_SQLselect .= "news "; // table name
$readNews_SQLselect_Query = mysql_query($readNews_SQLselect);
$indx = 1;
while ($row = mysql_fetch_array($readNews_SQLselect_Query, MYSQL_ASSOC)) {
$ID = $row['id'];
$CONTENT = $row['content'];
echo '<li>$indx." - ".$ID." ".$CONTENT."</li>';
$indx++;
}
mysql_free_result($readNews_SQLselect_Query);
I'm 99% sure is the syntax issue with echo '<li>$indx." - ".$ID." ".$CONTENT."</li>';.
PHP doesn't substitute variables in single quoted strings.
echo "<li>$indx - $ID $CONTENT</li>";
replace
'<li>$indx." - ".$ID." ".$CONTENT."</li>';
with:
'<li>'. $indx .' - ' . $ID .' ' . $CONTENT . '</li>';
EDIT 1
or with:
"<li>$indx - $ID $CONTENT</li>";
I have the following which returns
"soccertennisfootball"
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
echo "{$row['interest']}";
}
Can somebody please explain how I can explode the data and assign it as variables? I've been looking around at tutorials but they all seem to have some sort of delimeter?
Ive tried the following oly its acting very weird and printing out multiple times?
the following...
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$interests[] = $row['interest'];
$interest1 = $interests[0];
$interest2 = $interests[1];
$interest3 = $interests[2];
print $interest1 . " - " . $interest2 . " - " . $interest3;
}
Prints out
"Tennis - Tennis - Footy - Tennis -Soccer - Footy"
They aren't a single string, you're just echoing it out. Store it somewhere. This code stores it in a new array. It may be useful but it really depends on what you want to do with it.
$interests = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$interests[] = $row['interest'];
}
In each iteration of the while loop, you can only access one of the results (first 'soccer', then 'tennis', then 'football'). In your second code block, you're trying to somehow access all three inside the while loop.
Instead, inside the while loop, push that single result into an array:
$interests = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$interests[] = $row['interest'];
//First 'soccer' will be pushed into $interests, then 'tennis', then 'football'.
}
Now that you have all the values in the array, you can access each individually. This is done OUTSIDE the while loop:
echo $interests[0] . ' - ' . $interests[1] . ' - ' . $interests[2];
This will print "soccer - tennis - football".
Alternately, like some have said, you can loop through the $interests array and echo each value, or do whatever else you'd like with them.
Put them in an array, then do as you will with it after.
$interests = array();
while (...)
{
$interests[] = $row['interest'];
}
Use extract method from php http://php.net/manual/en/function.extract.php
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
extract($row);
echo $interest;
}
Use below snippet to store values in array
$interestsquery = "SELECT * FROM user_interests WHERE user_id = " . $usersClass->userID();
$result = mysql_query($interestsquery);
$result = array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$result[] = $row['interest']
}
print_r($result);
Given your clarifications (using MySQL, interest is the only column, and sample output) I would suggest using GROUP_CONCAT(). This puts it all on the MySQL side and allows you to pull the single row from $result.
Try the following sample query:
$query = "SELECT GROUP_CONCAT(interest SEPARATOR ' - ') FROM user_interests
WHERE user_id = " . $usersClass->userID() . "
GROUP BY user_id";