On Sunday 20 February 2005 00:52, Özgür Odabasi wrote: > hi all, > > I have trouble with preg_replace and querying database, > my code is below, > > <?php > include("h/h.php"); > > function getImgAttributes($id){ > $dsn = array( > 'phptype' => "mysql", > 'hostspec' => "localhost", > 'database' => "db", > 'username' => "root", > 'password' => "pass" > ); > > $myDbc = DB::connect($dsn); > > $myDbc->setFetchMode(DB_FETCHMODE_ASSOC); > $select_sql="select width, height, name from files where id='$id'"; > $execute_query = $myDbc->query($select_sql); > $row=$execute_query->fetchRow(); > > return 'id="'.$id.'" width="'.$row['width'].'" > height="'.$row['height'].'" alt="'.$row['name'].'"'; > } > > function writeImgTag ($text){ > $search = '<img id="(.*?)" />'; > $replace = 'div class="cimg"><img src="thumb.php?w=200&q=60&id=\1" > '.getImgAttributes("\\1").' /></div'; > $text = preg_replace ($search, $replace, $text); > echo $text; > } > > $text='<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. > Donec fermentum aliquam dolor.</p> > <img id="433" /> > <p>Pellentesque convallis, felis vel imperdiet tincidunt, urna augue > porttitor leo, eu luctus elit ante non mi.</p>'; > > writeImgTag($text); > > ?> > > > returns id but not DB result.... Why don't you return the $row hash instead of returning a string and then parsing it. > > <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec > fermentum aliquam dolor.</p> > > <div class="cimg"><img src="thumb.php?w=200&q=60&id=433" id="433" > width="" height="" alt="" /></div> > > <p>Pellentesque convallis, felis vel imperdiet tincidunt, urna augue > porttitor leo, eu luctus elit ante non mi.</p> -- Cyberly yours, Petar Nedyalkov Devoted Orbitel Fan :-) PGP ID: 7AE45436 PGP Public Key: http://bu.orbitel.bg/pgp/bu.asc PGP Fingerprint: 7923 8D52 B145 02E8 6F63 8BDA 2D3F 7C0B 7AE4 5436
Attachment:
pgpkAmCCAe66L.pgp
Description: PGP signature