Hello, I am writing a site, sections are used in the news, in the editor it is necessary to make a choice accordingly to which category the site belongs to.

The question is what to do

<select> <option>новости</option> <option>Π½Π΅ новости</option> <option>ΠΏΡ€ΠΎ новости</option> </select> 

Data is jerking from the database, including which category the news belongs to. How on html + pkhp to make an item that is received from the database "by default" so that you do not need to set the news category again when editing?

UPD:

 Mysql: table news id(autoink) date(datetime) autor(enum=admin,moderator) postPreview(varchar100) post(varchar255) category(enum=newscat1, newscat2, newscat3, no default) <? include(config); mysql_connect(host, log, pass); mysql_select_db(base); q=mysql_query("SELECT * FROM news WHERE id=$_GET[id]") while(FALSE!==(res=mysql_fetch_assoc(q)) { ?> <html> <form method=post> <select> <option>News Cat1</option> <option>News Cat2</option> <option>News Cat3</option> </select> <textarea><? echo $res[postpreview]; ?></textarea> <textarea><? echo $res[post]; ?></textarea> <input type=submit /> </form> 

If in general, that’s how it is now.

  • <a href=" tsya.ru"> http://tsya.ru </ a > - deamondz
  • thanks, I know Russian, sometimes it happens on autopilot) - DemoriaN
  • the question is closed, thank you all, who responded) - DemoriaN

4 answers 4

 $newscat = 15; // ΠΏΠΎΠ»ΡƒΡ‡ΠΈΡ‚ΡŒ ΠΈΠ· новости $news_arr = array( /* список сСлСкта , ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ Π² Π²ΠΈΠ΄Π΅: id: ΠΈΠ΄Π΅Π½Ρ‚ΠΈΡ„ΠΈΠΊΠ°Ρ‚ΠΎΡ€ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ, name: имя ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ (я Π½Π΅ знаю, ΠΊΠ°ΠΊ Ρƒ вас Π½Π°Π·Ρ‹Π²Π°ΡŽΡ‚ΡΡ эти поля, ΠΌΠΎΠΆΠ΅Ρ‚, Π±ΡƒΠ΄Π΅Ρ‚ $obj['categoryID'], Ссли катСгория - массив, Π° Π½Π΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚) */ ); echo '<select>'; foreach ($news_arr as $n => $obj) echo '<option value="'.$obj->id.'"'.($newscat==$obj->id?' selected="selected"':'').'>'.$obj->name.'</option>'; echo '</select>'; 

Record example:

 $cat = new stdClass(); $cat->id = 15; $cat->name = 'ΠšΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΡ Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€'; 

The same result will be returned by a query like

 $cat = mysql_fetch_object(mysql_query('SELECT id, name FROM ...')); 
  • it gets a list of news options from the array, and where does it match the one received from the database, does it "selected"? and where does the ID come from? even not so, the object is the name of the array, that is $ n and id - Demoria
  • Apparently I didn’t understand at all, where can Russian variables come from? What variables? - DemoriaN
  • Updated. > The data is jerking from the database, including which category the news belongs
  • An example of how an array should look like? that is, a seed for a couple of news IDs, let's say. - DemoriaN
  • so, okay, I did not understand anything, but it's cool) for my labors + and the correct answer, it remains for me to smoke the code)) everything seems to be clear, but nothing is clear)) - DemoriaN

There is an option to use the framework. If php framework, for example Zend Framework, then there it should be implemented by native methods. There is an option to do this through javascript in particular the jquery framework:

 $(document).ready(function(){ $('#yourSelectId').val('<?=$selectValue ?>'); }); 
  • the php + html model is interested in - DemoriaN
 $res = mysql_query('SELECT variant, default FROM variants'); $variants = array(); //массив Π² ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΏΠΎΠ»ΠΎΠΆΠΈΠΌ ΠΊΠ°Ρ‚Π΅Π³ΠΎΡ€ΠΈΠΈ while($row = mysql_fetch_assoc($res)) { $variants[] = $row; //собствСнно Π»ΠΎΠΆΠΈΠΌ... } 

Data is ready, we draw:

 include_once 'myTemplate.html'; /* * ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π°Π΅ΠΌ html ΡˆΠ°Π±Π»ΠΎΠ½Ρ‡ΠΈΠΊ(ΠΌΠΎΠΆΠ½ΠΎ прямо Π² скриптС) * Π½ΠΎ это ИМΠ₯О ΡƒΠ±ΠΎΠ³ΠΎ :) */ 

The template itself:

 <select> <?foreach($variants as $variant):?> <!-- Допустим Ρ‡Ρ‚ΠΎ Π΄Π΅Ρ„ΠΎΠ»Ρ‚Π½ΠΎΠ΅ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ Ρƒ нас всСгда = 1 Π° ΠΎΡΡ‚Π°Π»ΡŒΠ½Ρ‹Π΅ 0--> <?if($variant['default'] == 1):?> <option value="<?=$variant['variant']?>" selected><?=$variant['variant']?></option> <?endif?> <?if($variant['default'] == 0):?> <option value="<?=$variant['variant']?>"><?=$variant['variant']?></option> <?endif?> <?endforeach?> </select> 


if you understand correctly then here is how ...

  • one
    Yeah, are you by any chance one of the slaves of templating? :) <br> or your <br> <br> echo '<option value="'.$obj->id.'"'.($newscat==$obj->id?' > selected="selected"':'').'>'.$obj->name.'</option>'; >echo '</select>'; echo '<option value="'.$obj->id.'"'.($newscat==$obj->id?' > selected="selected"':'').'>'.$obj->name.'</option>'; >echo '</select>'; <br> cool written? xDDD <br> <br> Dear Kabbi php itself is a templating engine this time <br> two - I did not ask you how to draw a conclusion ... <br> three - I am not going to use the templating engine in a template engine - Zowie
  • 2
    I am not talking about that. You IF Indian, and design as in the books on php3 late 90s. If you read the link, there it is written that I do not digest template engines other than native ones. Just periodically, Joomla modules are so decorated, and the backlight works on {} and look for the appropriate endif wildly. - Sh4dow
  • one
    it is normal to separate the tabulation, not? <br> And why you touched on religious issues I don’t understand, for example, I don’t like it either, as you have written, but I don’t scream in comments that you wrote in Indian :) <br> Why this act agroschooling? - Zowie
  • 2
    Omfg, I say that you have extra operators (this is just about the Indian IF) and it is inconvenient to edit such code with more branching. In order to add an option to the parameter or change the value, it must be entered twice. With 4-5 consecutive conditions, it is difficult to search for the end of a branch even with illumination. This is not a matter of religion, but of quality. - Sh4dow
  • one
    If I began to describe how I organize the output into a template - my answer would be overwhelming, I described an algorithm for solving this problem, how to use it is everyone's business ... <br> I just know that absolutely most beginner programmers are soft saying not friendly with the ternary operator, which is why my answer looks like this ... I hope that the question has been settled ... - Zowie

Everything is simple to the shitless ...

 <select name="sex"> <option value="male" <?php if ($sex=='male') echo 'selected'; ?> >ΠœΡƒΠΆΡΠΊΠΎΠΉ</option> <option value="female" <?php if ($sex=='female') echo 'selected'; ?> >ЖСнский</option> </select> 

Thanks to all!