senters Posted September 14, 2011 Report Share Posted September 14, 2011 (edited) Kā iekš viena mainīgā ievietot vairākus mainīgos? Ideja ir tāda, lai vienmēr nebūtu iekš sql "steitmenta" jāraksta visas vajadzīgās kolonnas šādā veidā: $event_id = $row['id']; $event_name = $row['name']; $event_date_start = $row['date_start']; $event_time_start = $row['time_start']; $event_place = $row['place']; $event_date_created = $row['date_created']; gribētu tā vietā lietot vienu mainīgo $event_columns, kurš ietvertu sevī visas iespējamās tabulas kolonnas. Kopaina varētu būt šāda: $sql = mysql_query("select * from event where ...'"); while($row = mysql_fetch_assoc($sql)) { $event_id = $row['id']; $event_name = $row['name']; $event_date_start = $row['date_start']; $event_time_start = $row['time_start']; $event_place = $row['place']; $event_date_created = $row['date_created']; echo $event_columns; // sākas datu izvade echo "..."; Edited September 14, 2011 by senters Link to comment Share on other sites More sharing options...
codez Posted September 14, 2011 Report Share Posted September 14, 2011 (edited) $row ir asociatīvs masīvs un jau satur vairākus mainīgos, nekas nekur nav jāliek. Ja vēlies vienkārši attēlot visu $row saturu, tad print_r($row); vai, ja gribi vienkārši apvienot un atdalīt ar komatu tad echo implode(', ',$row); Un galu galā tu vari arī iterēt cauri visām $row vērtībām: foreach($row as $key=>$val){ echo $key.'='.$val.'<br />'; } Edited September 14, 2011 by codez Link to comment Share on other sites More sharing options...
senters Posted September 14, 2011 Author Report Share Posted September 14, 2011 Man vajag nodefinēt šīs rindas ($row) atbilstoši mainīgajiem: $event_id = $row['id']; $event_name = $row['name']; $event_date_start = $row['date_start']; $event_time_start = $row['time_start']; $event_place = $row['place']; $event_date_created = $row['date_created']; kurus pēc tam tālāk pielietoju datu izvadē. Link to comment Share on other sites More sharing options...
nemakuphp Posted September 14, 2011 Report Share Posted September 14, 2011 extract ar prefixu Link to comment Share on other sites More sharing options...
Maris-S Posted September 14, 2011 Report Share Posted September 14, 2011 Vai arī izmanto variable variables. $row['id'] = 1; $row['name'] = 'Vārds'; $row['date_start'] = 'Datums'; $row['time_start'] = 'Laiks'; $row['place'] = 'Vieta'; $row['date_created'] = 'Datums'; foreach ($row as $key => $value) { ${"event_$key"} = $value; } echo('<p>' . $event_id . '</p>'); echo('<p>' . $event_name . '</p>'); echo('<p>' . $event_date_start . '</p>'); echo('<p>' . $event_time_start . '</p>'); echo('<p>' . $event_place . '</p>'); echo('<p>' . $event_date_created . '</p>'); Link to comment Share on other sites More sharing options...
senters Posted September 14, 2011 Author Report Share Posted September 14, 2011 (edited) Izmēģināšu šo prefix metodi, izskatās labs variants. Jā, ar šo: foreach ($row as $key => $value) { ${"event_$key"} = $value; viss ļoti smuki strādā. Edited September 14, 2011 by senters Link to comment Share on other sites More sharing options...
briedis Posted September 14, 2011 Report Share Posted September 14, 2011 ${...} sucks, neviena normāla ide pēc tam nevarēs izsekot šādiem mainīgajiem :( Link to comment Share on other sites More sharing options...
senters Posted September 14, 2011 Author Report Share Posted September 14, 2011 Briedi, ko iesaki tā vietā lietot? Link to comment Share on other sites More sharing options...
briedis Posted September 14, 2011 Report Share Posted September 14, 2011 $event = mysql_fetch_assoc($data); echo $event['name']; Link to comment Share on other sites More sharing options...
senters Posted September 14, 2011 Author Report Share Posted September 14, 2011 (edited) Tas nozīmē šo: $data = mysql_query("select * from event ......."); while($row = mysql_fetch_assoc($data)) { foreach ($row as $key => $value) { ${"event_$key"} = $value;} echo "...$event_name....."; aizvietot uz: $data = mysql_query("select * from event ......."); $event = mysql_fetch_assoc($data); echo "...$event['name']....."; Vai pareizi sapratu? Edited September 14, 2011 by senters Link to comment Share on other sites More sharing options...
briedis Posted September 14, 2011 Report Share Posted September 14, 2011 (edited) Varbūt sāksim no paša sākuma? Kāpēc tev vajag tieši $mainigais_kautkas un neder $mainigais['kautkas']? Edited September 14, 2011 by briedis Link to comment Share on other sites More sharing options...
codez Posted September 14, 2011 Report Share Posted September 14, 2011 pievienojos briediem - kāda velna pēc vienus mainīgos pārsaukt par citiem. Link to comment Share on other sites More sharing options...
senters Posted September 14, 2011 Author Report Share Posted September 14, 2011 Pārtaisīju uz jau sākumā esošo struktūru, nepārsaucot mainīgos par citiem mainīgajiem. $data = mysql_query("select * from event where ..."); while($event = mysql_fetch_assoc($data)) { echo ".$event['image_small']." .... ".$event['name']." utt "; } Link to comment Share on other sites More sharing options...
daGrevis Posted September 14, 2011 Report Share Posted September 14, 2011 Starp citu, ja visa info par noteiktu lietu glabājas masīvā (t.i., $x['...']), nevis $x_..., $x_..., $x_... utt.)... vēlāk pašam būs vieglāk strādāt. Tieši tam masīvi ir domāti! Link to comment Share on other sites More sharing options...
senters Posted September 14, 2011 Author Report Share Posted September 14, 2011 Liels paldies visiem par zibenīgajām atbildēm, jā, palikšu pie masīviem. Patiesībā nav normālu argumentu kādēļ gribēju pa mainīgajiem pārdēvēt. Link to comment Share on other sites More sharing options...
Recommended Posts