How to grab only the first result mysql php - multiple queries inside foreach
I have a custom table in my wordpress database. In the database, I'm showing photo galleries with their own gallery ID's. Each gallery has multiple photos in it, so I might have:
image_link: img1.png | upload_id: 47372
image_link: img2.png | upload_id: 47372
image_link: img3.png | upload_id: 47372
image_link: img4.png | upload_id: 373h3
image_link: img5.png | upload_id: 373h3
I have code that returns the img id's and links the user to a URL where they can view the gallery:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays ORDER BY created_date ASC" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
$arr = "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
}
$unique_data = array_unique($arr);
// now use foreach loop on unique data
foreach($unique_data as $val)
{
echo $val;;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
Note that it's wrapped in a buffer, because it's inside a Wordpress shortcode, and the foreach stops return on the first result. I also use array_unique() to ensure that all results are unique (there's no need to show the same upload_id twice).
Now, here's the problem: I want to get the first and only the first image_link for each upload_id. When you stick echo "<br>" . $result->image_link; inside foreach ( $results as $result ), it prints every url that matches the id, and I can't use array_unique($arr); again because each img url is unique.
I think I can execute another DB query inside of foreach($unique_data as $val), using $val as the upload_id and grabbing the first result, but is there any way to adjust this so I don't have to? And if I do do that, will it be taxing on the server to run as many DB queries as I have foreach results?
php mysql wordpress
add a comment |
I have a custom table in my wordpress database. In the database, I'm showing photo galleries with their own gallery ID's. Each gallery has multiple photos in it, so I might have:
image_link: img1.png | upload_id: 47372
image_link: img2.png | upload_id: 47372
image_link: img3.png | upload_id: 47372
image_link: img4.png | upload_id: 373h3
image_link: img5.png | upload_id: 373h3
I have code that returns the img id's and links the user to a URL where they can view the gallery:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays ORDER BY created_date ASC" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
$arr = "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
}
$unique_data = array_unique($arr);
// now use foreach loop on unique data
foreach($unique_data as $val)
{
echo $val;;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
Note that it's wrapped in a buffer, because it's inside a Wordpress shortcode, and the foreach stops return on the first result. I also use array_unique() to ensure that all results are unique (there's no need to show the same upload_id twice).
Now, here's the problem: I want to get the first and only the first image_link for each upload_id. When you stick echo "<br>" . $result->image_link; inside foreach ( $results as $result ), it prints every url that matches the id, and I can't use array_unique($arr); again because each img url is unique.
I think I can execute another DB query inside of foreach($unique_data as $val), using $val as the upload_id and grabbing the first result, but is there any way to adjust this so I don't have to? And if I do do that, will it be taxing on the server to run as many DB queries as I have foreach results?
php mysql wordpress
add a comment |
I have a custom table in my wordpress database. In the database, I'm showing photo galleries with their own gallery ID's. Each gallery has multiple photos in it, so I might have:
image_link: img1.png | upload_id: 47372
image_link: img2.png | upload_id: 47372
image_link: img3.png | upload_id: 47372
image_link: img4.png | upload_id: 373h3
image_link: img5.png | upload_id: 373h3
I have code that returns the img id's and links the user to a URL where they can view the gallery:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays ORDER BY created_date ASC" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
$arr = "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
}
$unique_data = array_unique($arr);
// now use foreach loop on unique data
foreach($unique_data as $val)
{
echo $val;;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
Note that it's wrapped in a buffer, because it's inside a Wordpress shortcode, and the foreach stops return on the first result. I also use array_unique() to ensure that all results are unique (there's no need to show the same upload_id twice).
Now, here's the problem: I want to get the first and only the first image_link for each upload_id. When you stick echo "<br>" . $result->image_link; inside foreach ( $results as $result ), it prints every url that matches the id, and I can't use array_unique($arr); again because each img url is unique.
I think I can execute another DB query inside of foreach($unique_data as $val), using $val as the upload_id and grabbing the first result, but is there any way to adjust this so I don't have to? And if I do do that, will it be taxing on the server to run as many DB queries as I have foreach results?
php mysql wordpress
I have a custom table in my wordpress database. In the database, I'm showing photo galleries with their own gallery ID's. Each gallery has multiple photos in it, so I might have:
image_link: img1.png | upload_id: 47372
image_link: img2.png | upload_id: 47372
image_link: img3.png | upload_id: 47372
image_link: img4.png | upload_id: 373h3
image_link: img5.png | upload_id: 373h3
I have code that returns the img id's and links the user to a URL where they can view the gallery:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays ORDER BY created_date ASC" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
$arr = "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
}
$unique_data = array_unique($arr);
// now use foreach loop on unique data
foreach($unique_data as $val)
{
echo $val;;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
Note that it's wrapped in a buffer, because it's inside a Wordpress shortcode, and the foreach stops return on the first result. I also use array_unique() to ensure that all results are unique (there's no need to show the same upload_id twice).
Now, here's the problem: I want to get the first and only the first image_link for each upload_id. When you stick echo "<br>" . $result->image_link; inside foreach ( $results as $result ), it prints every url that matches the id, and I can't use array_unique($arr); again because each img url is unique.
I think I can execute another DB query inside of foreach($unique_data as $val), using $val as the upload_id and grabbing the first result, but is there any way to adjust this so I don't have to? And if I do do that, will it be taxing on the server to run as many DB queries as I have foreach results?
php mysql wordpress
php mysql wordpress
asked Nov 23 '18 at 12:33
Christian
4003726
4003726
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Please change your query to
"SELECT max(image_link) as image_link,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
But doesn't that just limit the ID's I can grab to 1? I don't want one result for the ID's, I want 1 url result PER ID, which in this case is 2–because there are two unique ID's.
– Christian
Nov 23 '18 at 12:41
Let me clear, you have different image_linl and unique_id. you want these image_link in one unique_id??? or one image_link for one unique_id???
– Sandeep Sudhakaran
Nov 23 '18 at 12:46
Each upload_id is displayed properly. In the DB, each upload_id has multiple image_link's attached to it. I want to show the first one for each upload_id.
– Christian
Nov 23 '18 at 12:48
Please try this query also. this will get the one image link per unique id. "SELECT max(image_link) as image_lin,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
– Sandeep Sudhakaran
Nov 23 '18 at 13:00
add a comment |
I solved this only 30 minutes after asking the question, but I worked on it for hours before asking, so for anyone else who comes across:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays GROUP BY upload_id" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
echo "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
echo $result->image_link;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
I got rid of array_unique($arr); and replaced it with GROUP BY upload_id in the query, which appears to ensure unique upload_id's and when used with echo $result->image_link;, only shows 1 link per unique upload_id.
Reference to this questions: wp get_results - filter unique records
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446807%2fhow-to-grab-only-the-first-result-mysql-php-multiple-queries-inside-foreach%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Please change your query to
"SELECT max(image_link) as image_link,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
But doesn't that just limit the ID's I can grab to 1? I don't want one result for the ID's, I want 1 url result PER ID, which in this case is 2–because there are two unique ID's.
– Christian
Nov 23 '18 at 12:41
Let me clear, you have different image_linl and unique_id. you want these image_link in one unique_id??? or one image_link for one unique_id???
– Sandeep Sudhakaran
Nov 23 '18 at 12:46
Each upload_id is displayed properly. In the DB, each upload_id has multiple image_link's attached to it. I want to show the first one for each upload_id.
– Christian
Nov 23 '18 at 12:48
Please try this query also. this will get the one image link per unique id. "SELECT max(image_link) as image_lin,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
– Sandeep Sudhakaran
Nov 23 '18 at 13:00
add a comment |
Please change your query to
"SELECT max(image_link) as image_link,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
But doesn't that just limit the ID's I can grab to 1? I don't want one result for the ID's, I want 1 url result PER ID, which in this case is 2–because there are two unique ID's.
– Christian
Nov 23 '18 at 12:41
Let me clear, you have different image_linl and unique_id. you want these image_link in one unique_id??? or one image_link for one unique_id???
– Sandeep Sudhakaran
Nov 23 '18 at 12:46
Each upload_id is displayed properly. In the DB, each upload_id has multiple image_link's attached to it. I want to show the first one for each upload_id.
– Christian
Nov 23 '18 at 12:48
Please try this query also. this will get the one image link per unique id. "SELECT max(image_link) as image_lin,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
– Sandeep Sudhakaran
Nov 23 '18 at 13:00
add a comment |
Please change your query to
"SELECT max(image_link) as image_link,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
Please change your query to
"SELECT max(image_link) as image_link,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
edited Nov 23 '18 at 13:02
answered Nov 23 '18 at 12:38
Sandeep Sudhakaran
33
33
But doesn't that just limit the ID's I can grab to 1? I don't want one result for the ID's, I want 1 url result PER ID, which in this case is 2–because there are two unique ID's.
– Christian
Nov 23 '18 at 12:41
Let me clear, you have different image_linl and unique_id. you want these image_link in one unique_id??? or one image_link for one unique_id???
– Sandeep Sudhakaran
Nov 23 '18 at 12:46
Each upload_id is displayed properly. In the DB, each upload_id has multiple image_link's attached to it. I want to show the first one for each upload_id.
– Christian
Nov 23 '18 at 12:48
Please try this query also. this will get the one image link per unique id. "SELECT max(image_link) as image_lin,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
– Sandeep Sudhakaran
Nov 23 '18 at 13:00
add a comment |
But doesn't that just limit the ID's I can grab to 1? I don't want one result for the ID's, I want 1 url result PER ID, which in this case is 2–because there are two unique ID's.
– Christian
Nov 23 '18 at 12:41
Let me clear, you have different image_linl and unique_id. you want these image_link in one unique_id??? or one image_link for one unique_id???
– Sandeep Sudhakaran
Nov 23 '18 at 12:46
Each upload_id is displayed properly. In the DB, each upload_id has multiple image_link's attached to it. I want to show the first one for each upload_id.
– Christian
Nov 23 '18 at 12:48
Please try this query also. this will get the one image link per unique id. "SELECT max(image_link) as image_lin,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
– Sandeep Sudhakaran
Nov 23 '18 at 13:00
But doesn't that just limit the ID's I can grab to 1? I don't want one result for the ID's, I want 1 url result PER ID, which in this case is 2–because there are two unique ID's.
– Christian
Nov 23 '18 at 12:41
But doesn't that just limit the ID's I can grab to 1? I don't want one result for the ID's, I want 1 url result PER ID, which in this case is 2–because there are two unique ID's.
– Christian
Nov 23 '18 at 12:41
Let me clear, you have different image_linl and unique_id. you want these image_link in one unique_id??? or one image_link for one unique_id???
– Sandeep Sudhakaran
Nov 23 '18 at 12:46
Let me clear, you have different image_linl and unique_id. you want these image_link in one unique_id??? or one image_link for one unique_id???
– Sandeep Sudhakaran
Nov 23 '18 at 12:46
Each upload_id is displayed properly. In the DB, each upload_id has multiple image_link's attached to it. I want to show the first one for each upload_id.
– Christian
Nov 23 '18 at 12:48
Each upload_id is displayed properly. In the DB, each upload_id has multiple image_link's attached to it. I want to show the first one for each upload_id.
– Christian
Nov 23 '18 at 12:48
Please try this query also. this will get the one image link per unique id. "SELECT max(image_link) as image_lin,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
– Sandeep Sudhakaran
Nov 23 '18 at 13:00
Please try this query also. this will get the one image link per unique id. "SELECT max(image_link) as image_lin,upload_id FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays group by upload_id ORDER BY created_date ASC"
– Sandeep Sudhakaran
Nov 23 '18 at 13:00
add a comment |
I solved this only 30 minutes after asking the question, but I worked on it for hours before asking, so for anyone else who comes across:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays GROUP BY upload_id" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
echo "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
echo $result->image_link;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
I got rid of array_unique($arr); and replaced it with GROUP BY upload_id in the query, which appears to ensure unique upload_id's and when used with echo $result->image_link;, only shows 1 link per unique upload_id.
Reference to this questions: wp get_results - filter unique records
add a comment |
I solved this only 30 minutes after asking the question, but I worked on it for hours before asking, so for anyone else who comes across:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays GROUP BY upload_id" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
echo "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
echo $result->image_link;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
I got rid of array_unique($arr); and replaced it with GROUP BY upload_id in the query, which appears to ensure unique upload_id's and when used with echo $result->image_link;, only shows 1 link per unique upload_id.
Reference to this questions: wp get_results - filter unique records
add a comment |
I solved this only 30 minutes after asking the question, but I worked on it for hours before asking, so for anyone else who comes across:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays GROUP BY upload_id" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
echo "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
echo $result->image_link;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
I got rid of array_unique($arr); and replaced it with GROUP BY upload_id in the query, which appears to ensure unique upload_id's and when used with echo $result->image_link;, only shows 1 link per unique upload_id.
Reference to this questions: wp get_results - filter unique records
I solved this only 30 minutes after asking the question, but I worked on it for hours before asking, so for anyone else who comes across:
$results = $wpdb->get_results( "SELECT * FROM wp_prefix_photos WHERE user_email='" . $email . "' AND created_date > $sevenDays GROUP BY upload_id" );
ob_start();
$arr = array();
foreach ( $results as $result )
{
echo "<div style='border: 1px solid black'><a href='https://mywebsite.com/?photoID=" . $result->upload_id . "'>https://mywebsite.com/?photoID=" . $result->upload_id . "</a></div>";
echo $result->image_link;
}
$output = ob_get_clean(); // set the buffer data to variable and clean the buffer
return $output;
I got rid of array_unique($arr); and replaced it with GROUP BY upload_id in the query, which appears to ensure unique upload_id's and when used with echo $result->image_link;, only shows 1 link per unique upload_id.
Reference to this questions: wp get_results - filter unique records
answered Nov 23 '18 at 13:03
Christian
4003726
4003726
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53446807%2fhow-to-grab-only-the-first-result-mysql-php-multiple-queries-inside-foreach%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown