PHP Facebook FQL Multquery and dealing with foreach and variables - php

Alright, so I am trying to figure this out. Basically I have everything working well except for adding the cover photos to each of the DIVS. Basically I have 2 queries using FQL, and here is my code.
$query =
'{"query1":"SELECT aid, cover_pid, name, description FROM album WHERE owner=$fbowner",
"query2":"SELECT src FROM photo WHERE pid IN (SELECT cover_pid FROM #query1)"}';
$fqlResult = $facebook->api(array(
'method' => 'fql.multiquery',
'queries' => $query));
$fql = $fqlResult[0]['fql_result_set'];
$covers = $fqlResult[1]['fql_result_set'];
$the_count = count($fql);
$i = 0;
foreach($fql as $value) {
$album_cover = $value['src'];
echo "<div class='fb_block'>";
echo "<a href='" . $url . "?action=list_pics&aid=" . $value['aid'] . "&album_name=" . $value['name'] . "'>";
echo "<img src='{$covers[$i]['src']}' border='1'>";
echo "</a>";
echo "<h3>".$value['name']."</h3>";
echo "<p>".$value['description']."</p>";
echo "</div>";
$i++;
}
Now that works fine, but what if I put this in the mix, the cover photos are all off unless I make $i = 1.
$i = 0;
foreach($fql as $value) {
if($value['name'] != 'Wall Photos'){
$album_cover = $value['src'];
echo "<div class='fb_block'>";
echo "<a href='" . $url . "?action=list_pics&aid=" . $value['aid'] . "&album_name=" . $value['name'] . "'>";
echo "<img src='{$covers[$i]['src']}' border='1'>";
echo "</a>";
echo "<h3>".$value['name']."</h3>";
echo "<p>".$value['description']."</p>";
echo "</div>";
$i++;
}
}
and if I stick another foreach in the mix, it grabs all the cover photos and sticks them into each div. So what is the easiest solution to just go in order and grab the cover photo associated with the album node?
Thanks in advance!

I tried this, too. You can't rely on the indexes being consistent between these two queries.
To make it work, first modify query2 to also return pid, then replace this line in your code:
echo "<img src='{$covers[$i]['src']}' border='1'>";
with this:
foreach($covers as $cover) {
if ($value['cover_pid'] === $cover['pid']) {
echo "<img src='{$cover['src']}' border='1'>";
break;
}
}

Related

How can i display my preg_match results in various divs so they can have multiple results under the correct heading?

Forgive me if this is simple but I have a for each loop that searches JSON data for results from a search. I then have some preg_match statements that will look at some of the tags within the JSON and if their is a match display the thumbnail in a div. and currently this all works. But it currently displays every result in its own Div and i want just five divs with multiple images within if there is a match.
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
if(preg_match("/_cm/i",$target)) {
echo '<div id="div5">';
echo '<h2>Creative</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SS/i",$target)) {
echo '<div id="div6">';
echo '<h2>Styled</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/_SH/i",$target)) {
echo '<div id="div7">';
echo '<h2>Group</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/still life/i",$target1) && preg_match("/_sm_00|_sd_00/i",$target)) {
echo '<div id="div8">';
echo '<h2>Cutout</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
if(preg_match("/worn/i",$target1)) {
echo '<div id="div9">';
echo '<h2>Worn</h2>';
echo "<img src='" . $hit->thumbnailUrl . "' alt='error'>";
echo $hit->metadata->baseName;
echo '</div>';
}
}
I cant quite figure out how to accomplish this, would it be the case of putting the results into an array and then displaying the results within the div?
Any help would be greatly appreciated.
You are right and answered the question yourself :)
Collect the results in a first step and create the markup in a second step. Something like this will do it:
$creative = [];
$styled = [];
/* ... */
function getHitInfos($theHit)
{
return [
"url" => $theHit->thumbnailUrl,
"name" => $theHit->metadata->baseName
];
}
function printResults($results, $title) {
echo '<div>';
echo '<h2>'.$title.'</h2>';
foreach ($results as $key => $val) {
echo "<img src='" . $val["url"] . "' alt='error'>";
echo $val["name"];
}
echo '</div>';
}
foreach ($hits as $hit)
{
$target = $hit->metadata->baseName;
$target1 = $hit->metadata->cf_imageType;
echo '<div>';
if(preg_match("/_cm/i",$target)) {
$creative[] = getHitInfos($hit)
}
if(preg_match("/_SS/i",$target)) {
$styled[] = getHitInfos($hit)
}
/* ... */
}
printResults($creative, "Creative");
printResults($styled, "Styled");
/* ... */
Disclaimer: My last contact with php is some years ago, but I hope you will see the point here.
(I also created some helping functions to DRY the code)

php echo iframe or image

Hey everyone I am having an issue with echoing an image or an iframe, the idea is there is an image link or an iframe in the same row in the database & i want to only echo what the string content is, if it echos the iframe there is a blank space of an image & underneath is the youtube video, if it echos the image the iframe does not show, they both currently echo the same row id labled url, I only want one to echo based on the string content instead of both echo's.
Here is the code I use to fetch the stuff, its only the echo area you want to pay attention to, I am not sure what kind of, if or else statement i could put in here.
< ?php
require_once("config.php");
$limit = (intval($_GET['limit']) != 0 ) ? $_GET['limit'] : 4;
$offset = (intval($_GET['offset']) != 0 ) ? $_GET['offset'] : 0;
$sql = "SELECT * FROM bookmarks WHERE 1 ORDER BY id DESC LIMIT $limit OFFSET
$offset";
try {
$stmt = $DB->prepare($sql);
$stmt->execute();
$results = $stmt->fetchAll();
} catch (Exception $ex) {
echo $ex->getMessage();
}
if (count($results) > 0) {
foreach ($results as $row) {
echo '<li><kef>' . $row['title'] . '</kef></br>';
echo '<img src=' . $row['url'] . '></br>'; /*here */
echo '<kef1>' . $row['url'] . '</kef1></br>'; /*and here*/
echo '<kef2>' . $row['desc'] . '</kef2></br>';
echo '<kef3>' . $row['tag'] . '</kef3></br> </br> </li>';
} }
?>
This script is a get results php
The best solution would be to save the data in different columns in the database. But if you want to keep the current structure would be able to check if the string contains HTML, then it is an iframe. Otherwise it is a link to the image.
<?php
if($row['url'] == strip_tags($row['url'])) {
//No HTML in string = link
echo '<img src="' . $row['url'] . '" alt="" />';
} else {
//Contains HTML = iframe
echo $row['url'];
}
?>
This assumes that you in your database have saved the values as either http://www.exempel.com or <iframe src =" http: // www ... ">. If the iframe-value also consists only of a URL, you must use another solution.
If you know whether the url is an image or video when you save them in the database then you could add another field for the media type. You could then use this in an if statement in your code, for example:
if (count($results) > 0) {
foreach ($results as $row) {
echo '<li><kef>' . $row['title'] . '</kef></br>';
if ($row['mediaType'] === 'image') {
echo '<img src=' . $row['url'] . '></br>'; /*here */
} if ($row['mediaType'] === 'video') {
echo '<kef1>' . $row['url'] . '</kef1></br>'; /*and here*/
}
echo '<kef2>' . $row['desc'] . '</kef2></br>';
echo '<kef3>' . $row['tag'] . '</kef3></br> </br> </li>';
}
}

PHP and MYSQLI with LIKE array: does not echo all content correctly

I have come this far and seem to be stuck on this trivial output issue, apologies in advance.
The $getexhibitions is an array and works when only this aspect of code is running. I have then added $results based on a LIKE keyword link that is intended to be an array within the array.
My problem is that the output is supposed to show $getexhibitions->title with the list of each $results->sponsor underneath. Java then creates a drop down with specific sponsor information. This will then repeat for the preceeding 8 years as so.
As you can see the code is getting stuck and i feel as though this may be a div or { problem or it may be more with the array code. Thanks in advance.
http://tenmoregirls.com/tenmoregirls/sponsors.php
<?php
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
$query_getexhibitions = $mysqli->query("SELECT * FROM exhibitions ORDER BY year DESC");
if ($query_getexhibitions) {
while($getexhibitions = $query_getexhibitions->fetch_object()) {
echo "<a>$getexhibitions->year | $getexhibitions->title</a>", "<br />";
$sponsorlink = $getexhibitions->year;
$result = $mysqli->query("SELECT * FROM sponsors WHERE (`year` LIKE '%".$sponsorlink."%')");
if ($result) {
while($results = $result->fetch_object()) {
echo "<a href = \"javascript:void(0)\" class=\"subheading\" onclick = \"document.getElementById($results->id).style.display='block'\" >$results->sponsor</a>", "<br />";
echo "<div id=\"$results->id\" style=\"display: none;\">
<img src=\"exhibitstyles/links/close.png\" alt=\"close\" width=\"20\"/><br />";
echo '<img src="sponsorimages/'.$results->logo.'">';
echo $results->summary, "<br />";
echo "$results->website", "<br /><br /><br />";
} "</div>"; } "</div>";
}} exit(); ?>
It's probably not a problem with PHP and MYSQLI, but just with a wrong closing div tag, which is outside the loop. There is also one additional closing tag that don't seem to refer to anything. Try to move </div> tag inside the loop and delete the other one.
Edit:
You were also missing echo command before "</div>"; tag, because you used ; on the end of the previous line. You also can't use , between multiple echo parameters. . is a concatenation operator for strings in php. Whereas . is a concatenation operator for strings, echo construction also allows the usage of , between arguments. My bad.
Try this:
while($getexhibitions = $query_getexhibitions->fetch_object()) {
echo "<a>$getexhibitions->year | $getexhibitions->title</a><br />";
$sponsorlink = $getexhibitions->year;
$result = $mysqli->query("SELECT * FROM sponsors WHERE (`year` LIKE '%".$sponsorlink."%')");
if ($result) {
while($results = $result->fetch_object()) {
echo "<a href = \"javascript:void(0)\" class=\"subheading\" onclick = \"document.getElementById($results->id).style.display='block'\" >$results->sponsor</a><br />";
echo "<div id=\"$results->id\" style=\"display: none;\">
<img src=\"exhibitstyles/links/close.png\" alt=\"close\" width=\"20\"/><br />";
echo '<img src="sponsorimages/'.$results->logo.'">';
echo $results->summary . "<br />";
echo "$results->website<br /><br /><br />";
echo "</div>";
}
}
}

Separate MySQL results into Divs using while loop

Just beginning PHP to bear with me.
Results I'm trying to achiever:
I have a table of YouTube URL's and MetaData.
Trying to build this:
<div class="slide">
<iframe></iframe>
<iframe></iframe>
</div>
<div class="slide">
<iframe></iframe>
<iframe></iframe>
</div>
Two videos per slide, then I'm going to paginate through results using Deck.js.
I suspect I'm going about this completely the wrong way, not that experienced at programmin g logic;
while($data = mysql_fetch_array($result)) {
for ($counter = 1; $counter<=2; $counter++) {
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
/* If Video 1, increment counter for 2nd video */
if ($counter == 1) {
$counter++;
}
/* If Video 2, close div and reset counter */
else if ($counter == 2) {
echo "</div>";
$counter = 1;
}
/* If error break out */
else {
echo "</div>";
break;
}
}
}
Basically trying to nest loops to keep track of how many videos per div and start a new one when a div has two.
I've tried a few different ways, this being the latest. Results in:
<div class="slide">
<iframe></iframe>
<div class="slide>
<iframe></iframe>
Hit the blank wall now, not sure what to try next. Willing to use/learn any method to accomplish the results, just not sure where to go at this point.
Cheers.
You could remove the second loop all together using the % operator (modulus). The idea is that a % b === 0 then the number a was evenly divisible by b. Using this, you can easily check for even or odd or every Nth row.
$k = 1;
echo "<div class=\"slide\">";
while($data = mysql_fetch_array($result)) { // you should really change to mysqli or PDO
if($k % 3 === 0){
echo "</div>";
echo "<div class=\"slide\">";
}
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
$k++;
}
echo "</div>";
Put the echo <div> before the for loop (still inside the while loop) and the </div> after the for loop
In your while loop you're retrieving just one row, but then you're iterating over it twice with a nested loop. Do away with the inner loop and just use a flip-flop variable to track left and right. I think this will do what you want:
$left=true; // track whether we're emitting HTML for left or right video
while($data = mysql_fetch_array($result)) {
if ($left) {
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
}
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
if (!$left) {
echo "</div>";
}
$left = !$left; // invert $left to indicate we're emitting the right iFrame
}
// end of loop. If we had an odd number of
// videos, tidy up the HTML
if (!$left) {
echo "</div>";
}
PHPFiddle
<?php
$x = 10;
$counter = 0;
while($x > 0)
{
if($counter != 0 && $counter % 2 == 0)
{
echo "ENDOFSLIDE</br>";
}
if($counter == 0 || $counter % 2 == 0)
{
echo "SLIDE</br>";
}
echo "iframe => $x </br>";
$x--;
$counter++;
}
echo "ENDOFSLIDE";
?>
It won't work because the for loop is inside the fetch loop for the SQL data. The second iteration of the for loop does not have a new SQL row. A better solution would be to capture the common column that identifies the two videos (the title) and generate a new div whenever that value changes. Try something like this, which will work for any number of SQL rows with the same title. This will also give proper results if the SQL query returns no rows and will handle the potential of a title with only one URL - which could get ugly if you merely flip-flop and end up with URLs on the wrong title. Of course, as in your current solution, your SQL query must ORDER BY VIDEO_TITLE so the rows are adjacent. I didn't run it, but should be close.
$lastTitle = "";
$n = 0; //count SQL rows processed
while($data = mysql_fetch_array($result)) {
// See if the title changed from last
if( $data['VIDEO_TITLE'] != $lastTitle ) {
// if we processed any rows, must end the current div before starting a new one
if( $n > 0 )
echo "</div>";
echo "<div class=\"slide\">";
echo "<h3>" . $data['VIDEO_TITLE'] . "</h3>";
//save the title as last title
$lastTitle = $data['VIDEO_TITLE'];
}
$n++; //count the SQL row
echo "<iframe width=\"560\" height=\"315\" src=\"" . $data['VIDEO_URL'] . "\" frameborder=\"0\" allowfullscreen></iframe>";
}
if( $n > 0 )
echo "</div>";

PHP and HTML Echo

I want to query a result from a table. The problem I run into is if the text is very long it will not auto-wrap to another line"best way i can think to explain it". It displays the text on one line and will go way off to the side of the page if the text is long. How can I get the text to space correctly. Thanks
here is my code:
<?php
echo "<div class=\"item_list2\">";
$get_items = "SELECT * FROM items WHERE category='Art'";
$result = mysql_query($get_items);
//loop! loops through the multiple results of the $get_items query
while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<b>" . $item['item'] . "</b><br/>" . $item['email'] . "<br/>";
echo $item['price'] . "</b><br/>" . $item['category'] . "</b><br/>" . $item['extra'];
echo "<br/><a href='manage.php?delete=" . $item['id'] . "'><small>[Delete]</small></a><br/><br/>";
}
echo "</div>";
?>
Just use CSS to break it.
.container
{
word-wrap: break-word;
}
maybe you want to add..
nl2br($item['extra']);

Categories