impartie foreach in 2 si adaugare div-uri
#1
Posted 19 February 2019 - 18:10
am un array cu 20 de chestii
vreau sa pun 10 in area x si 16 in area y. problema e ca printre chestiile din area y trebuie sa deschid si sa inchid niste div-uri foreach ( $media_data as $data ) { if ( 'x' === $area ) { /* chestia 1 chestia 2 chestia 3 chestia 4 chestia 5 chestia 6 chestia 7 chestia 8 chestia 9 chestia 10 */ } if ( 'y' === $area ) { /* <div class="row"> chestia 5 chestia 6 chestia 7 chestia 8 chestia 9 chestia 10 chestia 11 chestia 12 chestia 13 chestia 14 chestia 15 </div> <div class="row"> chestia 16 chestia 17 chestia 18 chestia 19 chestia 20 </div> */ } } am incercat cu un counter, dar pe cele din y n-am reusit sa le asez asa cum am nevoie deci cum se face? Edited by MembruAnonim, 19 February 2019 - 18:19. |
#2
Posted 19 February 2019 - 18:28
foreach ( $media_data as $data ) nu vad unde folosesti $data ulterior... nu am inteles enuntul marturisesc.
|
#3
Posted 19 February 2019 - 18:35
Nu sunt sigur că am înțeles întrebarea, dar eu fac așa,
$e = 0; foreach ( $rows as $row ) { if ( $e === 10 ) { echo '</div> <div class="row">'; $e = 0; } else { $e++; } } se mai poate verifica asa if( $e % 10 == 0) { echo '</div> <div class="row">'; } $e++; Edited by zalyx, 19 February 2019 - 18:34. |
#4
Posted 19 February 2019 - 18:41
#5
Posted 19 February 2019 - 18:49
este simplu.
cum am explicat mai sus, $chestii = array(); $y = ''; $x = ''; $e = 0; foreach( $chestii as $chestie ) { if ( condiție pentru a identifica chestii pentru variabila x ) { $x .= $chestie; } if ( condiție pentru a identifica chestii pentru variabila y ) { $y .= $chestie; if ( $e % 10 === 0 ) { $y .= '</div> <div class="row">'; } $y .= $chestie; } $e++; } Edited by zalyx, 19 February 2019 - 18:50. |
#6
Posted 19 February 2019 - 19:37
unbrutus, on 19 februarie 2019 - 18:28, said:
fnu am inteles enuntul marturisesc. zalyx, on 19 februarie 2019 - 18:35, said:
Nu sunt sigur că am înțeles întrebarea pun aproape tot codu, poate asa se intelege function insta_fee( $area = '' ) { $page_id = '54353333'; $req_nodes = '{followers_count,profile_picture_url,username,media{comments_count,id,like_count,media_url,permalink,thumbnail_url}}'; $access_token = '5675556775675655'; $api_url = 'https://graph.facebook.com/v3.2/' . $page_id . '?fields=instagram_business_account' . $req_nodes . '&access_token=' . $access_token; $api_response = wp_remote_get( $api_url ); if ( is_wp_error( $api_response ) ) { // eroare 1 } else { $decode_response = json_decode( wp_remote_retrieve_body( $api_response ) ); if ( array_key_exists( 'instagram_business_account', $decode_response ) ) { if ( array_key_exists( 'media', $decode_response->instagram_business_account ) && count( $decode_response->instagram_business_account->media->data ) >= 20 ) { $media_data = array(); $media_num = 1; // array-u cu informatiile a 20 de postari foreach ( $decode_response->instagram_business_account->media->data as $the_data ) { $media_data[ $media_num ]['comments'] = $the_data->comments_count; $media_data[ $media_num ]['id'] = $the_data->id; $media_data[ $media_num ]['likes'] = $the_data->like_count; $media_data[ $media_num ]['media'] = $the_data->media_url; $media_data[ $media_num ]['permalink'] = $the_data->permalink; if ( array_key_exists( 'thumbnail_url', $the_data ) ) { $media_data[ $media_num ]['thumbnail'] = $the_data->thumbnail_url; } if ( 20 == $media_num++ ) { break; } } // partea care va fi printata in footer sau header echo '<div class="' . $area . '-insta-fee">'; foreach ( $media_data as $data ) { if ( 'header' === $area ) { // aici vreau sa pun de ex id-urile a primelor 10 postari: echo $data['id']; // rezultatul ar fi cam asa: /* ID POSTARE 1 ID POSTARE 2 ID POSTARE 3 ID POSTARE 4 ID POSTARE 5 ID POSTARE 6 ID POSTARE 7 ID POSTARE 8 ID POSTARE 9 ID POSTARE 10 */ } if ( 'footer' === $area ) { // aici vreau sa echo id-urile a postarilor 5, 6 samd pana la 20 // faza e ca printre postarile astea vreau sa pun niste div-uri // asta as vrea sa fie rezultatul /* <div class="empty-row"></div> <div class="row-1"> ID POSTARE 5 ID POSTARE 6 ID POSTARE 7 ID POSTARE 8 ID POSTARE 9 ID POSTARE 10 ID POSTARE 11 ID POSTARE 12 ID POSTARE 13 ID POSTARE 14 ID POSTARE 15 </div> <div class="row-2"> ID POSTARE 16 ID POSTARE 17 ID POSTARE 18 ID POSTARE 19 ID POSTARE 20 </div> */ } } echo '</div>'; } else { // eroare 2 } } elseif ( array_key_exists( 'error', $decode_response ) ) { if ( 10 == $decode_response->error->code ) { // eroare 3 } elseif ( 100 == $decode_response->error->code ) { // eroare 4 } elseif ( 104 == $decode_response->error->code ) { // eroare 5 } elseif ( 190 == $decode_response->error->code ) { // eroare 6 } elseif ( 803 == $decode_response->error->code ) { // eroare 7 } else { // eroare 8 } } else { // eroare 9 } } } insista forumul sa imi rearanjeze codul. mortii lui Edited by MembruAnonim, 19 February 2019 - 19:35. |
#7
Posted 19 February 2019 - 20:06
Cred că îți va fi mai ușor dacă la insta_fee vei adauga și parametrii $from și $to
foreach ( $decode_response->instagram_business_account->media->data as $the_data ) { // La toate adaugi așa $media_data[$media_num] = array( 'comments' => $the_data->comments_count, // restul ) } în al doilea foreach scrii foreach ( $media_data as $key => $data ) iar în interiorul său scrii if ($key < $from ) { continue; } if ($key > $to ) { break; } Iar pentru div-uri eu am explicat mai sus. Edited by zalyx, 19 February 2019 - 20:14. |
#8
Posted 19 February 2019 - 20:38
zalyx, on 19 februarie 2019 - 20:06, said: asta cum o adaug in array-ul sugerat de tine? if ( array_key_exists( 'thumbnail_url', $the_data ) ) { $media_data[ $media_num ]['thumbnail'] = $the_data->thumbnail_url; } imi apare Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) cand incerc asa: ( array_key_exists( 'thumbnail_url', $the_data ) ) ? 'thumbnail' => $the_data->thumbnail_url |
#9
Posted 19 February 2019 - 20:44
MembruAnonim, on 19 februarie 2019 - 20:38, said:
asta cum o adaug in array-ul sugerat de tine? if ( array_key_exists( 'thumbnail_url', $the_data ) ) { $media_data[ $media_num ]['thumbnail'] = $the_data->thumbnail_url; } imi apare Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) cand incerc asa: ( array_key_exists( 'thumbnail_url', $the_data ) ) ? 'thumbnail' => $the_data->thumbnail_url arată-mi codul. |
#10
Posted 19 February 2019 - 20:46
zalyx, on 19 februarie 2019 - 20:44, said: foreach ( $decode_response->instagram_business_account->media->data as $the_data ) { $media_data[ $media_num ] = array( 'comments' => $the_data->comments_count, 'id' => $the_data->id, 'likes' => $the_data->like_count, 'media' => $the_data->media_url, 'permalink' => $the_data->permalink, ( array_key_exists( 'thumbnail_url', $the_data ) ) ? 'thumbnail' => $the_data->thumbnail_url ); if ( 20 == $media_num++ ) { break; } } |
|
#11
Posted 19 February 2019 - 20:58
MembruAnonim, on 19 februarie 2019 - 20:46, said: ( array_key_exists( 'thumbnail_url', $the_data ) ) ? 'thumbnail' => $the_data->thumbnail_url aceste verificări trebuie să fie deasupra la $media_data[ $media_num ] =. Instagram are și album și pentru album $the_data->thumbnail_url nu este, 'thumbnail' => $the_data->thumbnail_url Cu media_type poți verifica dacă este poză, video sau album |
#12
Posted 19 February 2019 - 21:14
zalyx, on 19 februarie 2019 - 20:58, said:
aceste verificări trebuie să fie deasupra la $media_data[ $media_num ] =. Instagram are și album și pentru album $the_data->thumbnail_url nu este, 'thumbnail' => $the_data->thumbnail_url Cu media_type poți verifica dacă este poză, video sau album thumbnail_url e pentru video daca e postare de tip galerie media_url contine url-ul catre prima imagine a galeriei Edited by MembruAnonim, 19 February 2019 - 21:15. |
#13
Posted 19 February 2019 - 21:36
Nu este corect să adaugi ( array_key_exists( 'thumbnail_url', $the_data ) ) ? 'thumbnail' => $the_data->thumbnail_url în array.
if ( array_key_exists( 'thumbnail_url', $the_data ) ) { $media_data[ $media_num ]['thumbnail'] = $the_data->thumbnail_url; } |
#14
Posted 19 February 2019 - 21:37
#15
Posted 19 February 2019 - 21:45
|
#16
Posted 20 February 2019 - 16:42
zalyx, on 19 februarie 2019 - 21:36, said:
eu voiam sa folosesc functia insta_fee() in header.php si footer.php (pe un template wordrpess) acum m-am gandit ca asta inseamna ca asta wp_remote_get( $api_url ), se executa de doua ori daca fac de exemplu asa, e aceeasi chestie? function instapi() { $page_id = 'gggg'; $req_nodes = '{followers_count,profile_picture_url,username,media{comments_count,id,like_count,media_url,permalink,thumbnail_url}}'; $access_token = 'jjjjjjjjjjj'; $api_req = 'https://graph.facebook.com/v3.2/' . $page_id . '?fields=instagram_business_account' . $req_nodes . '&access_token=' . $access_token; $api_response = wp_remote_get( $api_req); return $api_response; } // functia pe care o s-o pun in header.php function insta_header() { $decode_response = json_decode( wp_remote_retrieve_body( instapi() ) ); } // functia pe care o s-o pun in footer.php function insta_footer() { $decode_response = json_decode( wp_remote_retrieve_body( instapi() ) ); } edit: e acelasi lucru. daca declar o functie in interiorul instapi() imi apare Fatal error: Cannot redeclare functia_declarata() Edited by MembruAnonim, 20 February 2019 - 16:51. |
#17
Posted 20 February 2019 - 20:23
MembruAnonim, on 20 februarie 2019 - 16:42, said:
eu voiam sa folosesc functia insta_fee() in header.php si footer.php (pe un template wordrpess) acum m-am gandit ca asta inseamna ca asta wp_remote_get( $api_url ), se executa de doua ori daca fac de exemplu asa, e aceeasi chestie? function instapi() { $page_id = 'gggg'; $req_nodes = '{followers_count,profile_picture_url,username,media{comments_count,id,like_count,media_url,permalink,thumbnail_url}}'; $access_token = 'jjjjjjjjjjj'; $api_req = 'https://graph.facebook.com/v3.2/' . $page_id . '?fields=instagram_business_account' . $req_nodes . '&access_token=' . $access_token; $api_response = wp_remote_get( $api_req); return $api_response; } // functia pe care o s-o pun in header.php function insta_header() { $decode_response = json_decode( wp_remote_retrieve_body( instapi() ) ); } // functia pe care o s-o pun in footer.php function insta_footer() { $decode_response = json_decode( wp_remote_retrieve_body( instapi() ) ); } edit: e acelasi lucru. daca declar o functie in interiorul instapi() imi apare Fatal error: Cannot redeclare functia_declarata() Folosește o clasă și nu se va executa de două ori. Edited by zalyx, 20 February 2019 - 20:39. |
#18
Posted 21 February 2019 - 04:43
Anunturi
▶ 0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users