PHP show data from MySQL - Theatre seats - php

I'm programming a map of theatre seats.
With this code I try to show selected seats on a previous page, but I can't show all of the seats in a color and in another color the selected ones.
It shows all of the seats and only one selected.
Then all of the seats again, and another selected.
And so on...
Any suggestions?
Thank you very much and here's the piece of code:
foreach ($AsientosSeleccionadosI as $claveAI=>$valorAI) {
$resultButacas = mysql_query("SELECT U.Id AS IdUbicacion, U.IdModoTeatro, U.Numero, U.Fila, U.IdSector, U.CordX, U.CordY,
P.IdEspectaculo, P.IdFuncion, P.IdSector, P.Precio
FROM TeatroAsiento U, PrecioEspectaculoSector P
WHERE U.IdSector = '$Sector'
AND P.IdSector = '$Sector'
AND P.IdFuncion = '$fn'
AND P.IdEspectaculo = '$id'
");
while($rowButacas=mysql_fetch_array($resultButacas)){
$fila = $rowButacas["Fila"];
$asiento = $rowButacas["Numero"];
$IdUbicacion = $rowButacas["IdUbicacion"];
$precio = $rowButacas["Precio"];
echo '<div id="Casilla"><label for="asiento'.$fila.''.$asiento.'">';
if($IdUbicacion == $valorAI){
echo '<div id="ubicacion" style="background-image: url(../css/images/asientosReservado.png);"><input id="asiento'.$fila.''.$asiento.'" value="'.$IdUbicacion.'" name="asientoI[]" type="hidden"><input id="'.$IdUbicacion.''.$precio.'" value="'.$precio.'" type="hidden" name="precioseleccionI[]">';
}else{
echo '<div id="ubicacion" style="background-image: url(../css/images/asientosDisponible.png);">';
}
echo 'F: '.$fila.'<br>Nro: '.$asiento.'<br>'.$precio.'';
echo '</div></label></div>';
}
}

If I got your logic correctly IMHO you don't need loop-wrapper.
You just need to check if value $IdUbicacion exists in your array $AsientosSeleccionadosI :
$resultButacas = mysql_query("SELECT U.Id AS IdUbicacion, U.IdModoTeatro, U.Numero, U.Fila, U.IdSector, U.CordX, U.CordY,
P.IdEspectaculo, P.IdFuncion, P.IdSector, P.Precio
FROM TeatroAsiento U, PrecioEspectaculoSector P
WHERE U.IdSector = '$Sector'
AND P.IdSector = '$Sector'
AND P.IdFuncion = '$fn'
AND P.IdEspectaculo = '$id'
");
while ($rowButacas = mysql_fetch_array($resultButacas)) {
$fila = $rowButacas["Fila"];
$asiento = $rowButacas["Numero"];
$IdUbicacion = $rowButacas["IdUbicacion"];
$precio = $rowButacas["Precio"];
echo '<div id="Casilla"><label for="asiento' . $fila . '' . $asiento . '">';
if ( array_search($IdUbicacion,$AsientosSeleccionadosI)!= false) {
echo '<div id="ubicacion" style="background-image: url(../css/images/asientosReservado.png);">'
. '<input id="asiento' . $fila . '' . $asiento . '" value="' . $IdUbicacion . '" name="asientoI[]" type="hidden">'
. '<input id="' . $IdUbicacion . '' . $precio . '" value="' . $precio . '" type="hidden" name="precioseleccionI[]">';
} else {
echo '<div id="ubicacion" style="background-image: url(../css/images/asientosDisponible.png);">';
}
echo 'F: ' . $fila . '<br>Nro: ' . $asiento . '<br>' . $precio . '';
echo '</div></label></div>';
}

Related

Array: Compare values from API with values from own database

I have built a list (moviesSeen) where people can save their seen movies.
Now I want to get the director for each movie from an API and then compare which movies have already been seen. The output of the movies works without problems, but I can't get the result from the database (moviesSeen) to match the movies of the director. How can I achieve this?
The goal of the script is to show all movies of a director and mark all the ones you have already seen.
function getCrewDetails() {
global $apiKey;
global $language;
$personID = htmlspecialchars($_GET['personID']);
$url = "https://api.themoviedb.org/3/person/" . $personID . "?api_key=" . $apiKey . "&" . $language . "";// path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$personPrivate = json_decode($data); // decode the JSON feed
echo '<div class="row">';
echo '<div class="col">';
echo '<img src="https://image.tmdb.org/t/p/w300/' . $personPrivate->profile_path . '" class="img-thumbnail">';
echo '</div>'; // end div col
echo '<div class="col-8">';
echo '<h1>' . $personPrivate->name . '</h1>';
if (!empty($personPrivate->biography)) {
echo $personPrivate->biography;
} else {
echo '<p>Verdammmt! Zu dieser Person liegen keine biographischen Details vor.</p>';
echo '<p>Dabei hätte sie es doch so verdient :(</p>';
}
echo '</div>'; // end div col
echo '</div>'; // end div row
// echo '<pre>';
// print_r($person);
$url = "https://api.themoviedb.org/3/person/" . $personID . "/movie_credits?api_key=" . $apiKey . "&" . $language . "";// path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$personCareer = json_decode($data); // decode the JSON feed
echo '<div class="container mt-4">';
echo "<h2>Filmographie</h2>";
echo '<table class="table table-striped table-sm">';
echo '<thead>';
echo '<tr>';
echo '<th scope="col">Film</th>';
echo '<th scope="col">Gesehen</th>';
echo '<th scope="col">Funktion</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
global $pdo;
$stmt = $pdo->prepare("SELECT * FROM movieSeen WHERE user_id = '" . $_SESSION['id'] . "'");
$stmt->execute();
foreach ($stmt as $row ) {
echo $row['movie_id'] . ', ';
} // output of the saved IDs in movieSeen
foreach ($personCareer->crew as $showCrewDetails) { //Output of the movies
echo '<tr>';
echo '<td>' . $showCrewDetails->title . ' ' . $showCrewDetails->id . ' (' . substr($showCrewDetails->release_date, 0, 4) . ')</td>';
echo '<td><span class="badge badge-danger">Movie Seen</span></td>';
echo '<td>' . $showCrewDetails->job . '</td>';
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
//echo '<pre>';
//var_dump($personCareer);
}
I know that I have to edit the last foreach loop, but I just can't get it to work :(

Facebook PHP SDK - Graph API Like

Can someone help me with this, if you go to: https://developers.facebook.com/tools/explorer and use Graph API, GET Method on Connections->Home - You can grab your news feed.
In that newsfeed, the array shows some information based on each individual feed item.
One thing on there is likes which has a sub value of count which counts the number of likes.
Problem is, when I change it to use my applications and access token, the information is still there, expect the count part.
This is really confusing, I have read_stream as a scope.
My code the fetch the likes is:
if(isset($news['likes']['count'])) $likes_count = $news['likes']['count'];
else $likes_count = 0;
Which is part of my foreach loop.
For good measure, here is the function:
function newsitem($profile_pic,$from,$to,$message,$picture,$name,$link,$caption,$description,$icon,$time,$comments,$likes)
{
if($to) $to_section = '<div class="to" >to</div><div class="news-friend-name"><strong><a>' . $to . '</a></strong></div><div class="clear"></div>';
else $to_section = '';
if($message) $message_section = '<div class="message">' . $message . '</div>';
else $message_section = '';
if($picture) $picture_section = '<div class="external-image"><img src="' . $picture . '"/></div><div class="news-external">';
else $picture_section = '<div class="news-external" style="width: 410px;">';
if(!$link) $link='#';
if($name) $name_section = '<div class="news-title"><h3>' . $name . '</h3></div>';
else $name_section = '';
if($caption) $caption_section = '<div class="news-caption"><i>' . $caption . '</i></div>';
else $caption_section = '';
if($description) $description_section = '<div class="news-desc">' . $description . '</div>';
else $description_section = '';
if($icon) $icon_section = '<div class="news-icon" ><img src="' . $icon . '" /></div>';
else $icon_section = '';
$time_converted = time_elapsed($time);
$news = '<div class="news">
<div class="news-friend-thumb"><img src="' . $profile_pic . '"/></div>
<div class="news-content">
<div class="news-friend-name"><strong><a>'. $from . '</a></strong></div>'.$to_section.
'<div class="clear"></div>' . $message_section . $picture_section . $name_section . $caption_section . $description_section .
'</div>
<div class="clear"></div>
<div class="comment-like">' . $icon_section . $time_converted . ' ago • ' . $comments . ' comments • ' . $likes . ' likes</div>
</div>
</div>';
return $news;
}`
I'd appreciate the help, thanks!

Keep getting Invalid argument supplied for foreach() in

I keep getting this warning:
Warning: Invalid argument supplied for foreach() in
here is my full code which performe function list output from mysql and paging it 10 output per page , the error appear in last page
<?php
require_once('dbconnect.php');
$yesterday = date("Y-m-d", strtotime("yesterday"));
$page=intval($_POST['p']);
if($page=='')
{
$page=1;
}
$dbadd=($page-1)*10;
$query = "SELECT * FROM ranking";
$totalposts=mysql_num_rows(mysql_query($query));
$totalpages=ceil($totalposts/10);
$query = "SELECT r.ranking,r.screenname,r.name,r.followers,r.tweets,r.location,r.`join date`,r.avatar, h.date, r.followers-h.followers followers_diff, r.tweets-h.tweets tweets_diff FROM ranking r, ranking_hist h WHERE r.screenname=h.screenname and h.date='$yesterday' AND r.ranking>$dbadd AND r.ranking<($dbadd+11) ORDER BY ranking ASC LIMIT 10 ";
$result = mysql_query($query);
if( !$result ) {
die("Error: " . mysql_error() );
}
while($row = mysql_fetch_assoc($result) ) {
$tweep = $row['screenname'];
$tweeps[$tweep] = $row;
}
$query = "SELECT r.ranking,r.screenname,r.name,r.followers,r.tweets,r.location,r.`join date`,r.avatar FROM ranking r WHERE r.screenname NOT IN ( SELECT DISTINCT screenname from ranking_hist ) AND r.ranking>$dbadd AND r.ranking<($dbadd+11) ORDER BY ranking ASC LIMIT 10";
$result = mysql_query($query);
if( !$result ) {
die("Error: " . mysql_error() );
}
while($row = mysql_fetch_assoc($result) ) {
echo "";
$tweep = $row['screenname'];
$tweeps[$tweep] = $row;
}
mysql_free_result($result);
$i = 0;
$total_amount = count($tweeps);
foreach ($tweeps as $tweep) {
$i++;
if ($total_amount == $i) {
$class = 'divrow divrowlast';
} else {
$class = 'divrow';
}
$col5 = "";
if( $tweep['followers_diff'] > 0 ) {
$col5 = "<span style='color:green; display:inline;'> +" . $tweep['followers_diff'] . "▲ </span>";
}
else if( $tweep['followers_diff'] < 0 ) {
$col5 = "<span style='color:red; display:inline;'> -" . $tweep['followers_diff'] . "▼ </span>";
}
$html_table .= '<div class="'.$class.'"><ul>' .
"<li class='row100rank'> " . $tweep['ranking'] . "</li>" .
"<li class='row100user'>
<div class='avatar'><img width='32' height='32' src='" . $tweep['avatar'] . "' alt='" . $tweep['screenname'] ."' /></div>
<div class='feature-author'><a class='text_bigger' href='http://www.twitter.com/" . $tweep['screenname'] . "/'>#".$tweep['screenname']."</a></div>
<div class='row100description'>".$tweep['name']."<br />".$tweep['location']."</div></li>" .
"<li class='pad'><div class='stat'> <span>" . $tweep['followers'] . " $col5</span> followers</div></li>" .
"<li class='pad rowTwitte'><div class='stat'> <span>" . $tweep['tweets'] . " $col6</span> tweets</div></li>" .
"<li class='pad rowJoionDate'><div class='stat'> <span>" . date ( 'd M y', strtotime($tweep['join date'])) . "</span> joindate</div></li>" .
"</ul></div>";
}
echo $html_table;
?>
<div class="dataTables_paginate paging_full_numbers" ><span class="first paginate_button" ><a class="pagelinks" href="javascript:void(0);" pageid="1" onClick="changepage(1);">First</a></span>
<span class="previous paginate_button" ><a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $page>1?($page-1):1;?>" onClick="changepage(<?php echo $page>1?($page-1):1;?>);" >Previous</a></span>
<span>
<?php
switch($page){
case 1:
$it=1;
$itl=6;
break;
case 2:
$it=1;
$itl=6;
break;
case $totalpages:
$it=$totalpages-4;
$itl=$totalpages+1;
break;
case $totalpages-1:
$it=$totalpages-4;
$itl=$totalpages+1;
break;
default:
$it=$page-2;
$itl=$page+3;
}
for(;$it<$itl;$it++){
?>
<span class="<?php echo $page== $it?'paginate_active':'paginate_button'; ?>">
<a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $it;?>" onClick="changepage(<?php echo $it;?>);" ><?php echo $it; ?></a></span>
<?php
}
?>
</span>
<span class="next paginate_button" ><a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $page<$totalpages?($page+1):$page;?>" onClick="changepage(<?php echo $page<$totalpages?($page+1):$page;?>);">Next</a>
</span>
<span class="last paginate_button" >
<a class="pagelinks" href="javascript:void(0);" pageid="<?php echo $totalpages;?>" onClick="changepage(<?php echo $totalpages;?>);">Last</a>
</span></div>
<div class="dataTables_info">
Showing <?php echo ($page-1)*10+1;?> to <?php echo ($page-1)*10+10;?> of <?php echo $totalposts;?>
</div>
Try the code blow. Your $tweeps might on some occasions not be an array or object, check if the array is not empty and is_array($tweeps).
$i = 0;
$total_amount = count($tweeps);
if(is_array($total_amount) && !empty($tweeps))
{
foreach ($tweeps as $tweep)
{
$i++;
if ($total_amount == $i)
{
$class = 'divrow divrowlast';
}
else
{
$class = 'divrow';
}
}
}
Your problem is likely to be that the query isn't retuning any data.
You are populating new entries into the $tweeps array inside the loop, but if there's no data, it won't ever go into that loop.
You aren't initialising the $tweeps variable anywhere else, so if it doesn't go into the loop, $tweeps won't be an array, it'll be undefined. This will then give you an error if you try to use it in a foreach().
You need to add a line to initialise the variable before you start populating it:
$tweeps = array();
This would go somewhere above the loop where you first start populating $tweeps.
Hope that helps.

PHP/WordPress: override page_id in foreach loop

I have a WordPress site and currently the code it set to create titles based on the page title. The titles are: Interior House Painting, Exterior House Painting and Commercial Painting. I would like to override the titles to remove the word "house". This is the code currently:
<?php
// interior_painting: 18
// exterior_painting: 25
// other services: 36
$page_ids = array(18, 25, 36);
$images = array('servicesInterior.jpg', 'servicesExterior.jpg', 'servicesOther.jpg');
foreach ($page_ids as $key => $page_id) {
$page_post = get_post($page_id);
$page_custom_key = 'home_page_info';
$page_link = $page_post->post_name;
$li_class = $page_id == 36 ? 'noMargin' : '';
$title = $page_id == 18 ? 'Interior Painting' : $page_post->post_title . " ";
$title = $page_id == 36 ? 'Commercial Projects' : $page_post->post_title . " ";
$title = $page_id == 25 ? 'Exterior Painting' : $page_post->post_title . " ";
echo '<li class="' . $li_class . '"> <a href="' . $page_link . '">';
echo '<img class="alignright size-full wp-image-349" title="servicesInterior" src="/wp-content/uploads/2011/04/' . $images[$key] . '" alt="" width="200" height="95" /></a>';
echo '<h2>' . $title . '</h2>';
echo get_post_meta($page_id, $page_custom_key, true);
echo '<a class="btn-find-more" href="' . $page_post->post_name . '">FIND OUT MORE</a></li>';
}
?>
The output is this: Interior House Painting, Exterior Painting, Commercial Painting. How do I get "house" removed from "Interior House Painting"?
$title = ucwords(trim(strtolower(str_replace('house', '', $page_post->post_title))));
Though, I am a little curious as to why you would not prefer to just remove the word from the title altogether.
ADDITIONALLY: Somebody else mentioned using preg_replace. That's also entirely possible, but it would be best to do a case-insensitive search:
$title = trim(preg_replace('/house/i', '', $page_post->post_title));
Just use preg_replace('House'|| 'house','',/*Variable Page title is in*/);
You could try replacing the line:
echo '<h2>' . $title . '</h2>';
With something like this:
if (is_page('Interior House Painting')) { echo '<h2>Interior Painting</h2>'; }
if (is_page('Exterior House Painting')) { echo '<h2>Exterior Painting</h2>'; }
else { echo '<h2>' . $title. '</h2>'; }

variable inside variable in a PHP function?

I have the following function.
The problem is that the $lang variable will vary depending on the $this->setts['site_lang']; . The actual problem is the following:
$condition_details['description_$lang']
(which isn't working). How do I get this to display
$condition_details['description_us']
or
$condition_details['description_fr']
depending on the $lang setting?
And here is the full function:
function shipped_drop_down($box_name = 'item_shipped_via', $selected = null, $form_refresh = null)
{
(string) $display_output = null;
$lang = $this->setts['site_lang'];
$sql_select_conditions = $this->query("SELECT id, description_".$lang." FROM " . DB_PREFIX . "advanced_shipping_carriers ORDER BY theorder,description_".$lang." ASC");
$display_output = '<select name="' . $box_name . '" ' . (($form_refresh) ? 'onChange = "submit_form(' . $form_refresh . ', \'\')"' : '') . ' style="font-size:10px; width: 120px;"> ';
while ($condition_details = $this->fetch_array($sql_select_conditions))
{
$display_output .= '<option value="' . $condition_details['id'] . '" ' . (($condition_details['id'] == $selected) ? 'selected' : '') . '>' . $condition_details['description_$lang'] . '</option> ';
}
$display_output .= '</select> ';
return $display_output;
}
You could do
$condition_details['description_'.$lang]
or use "
$condition_details["description_$lang"]
You could store your lang settings in arrays like this :
$lang['fr']['condition_details']
so you could use $lang[$selected_lang]['condition_details']
Use "double quotes" instead of 'single quotes'. That way the $lang will be parsed and replaced with the relevant value.
Use double quotes like so:
$condition_details["description_$lang"]

Categories